C++--第23课 - STL简介

第23课 - STL简介

1. 标准模板SLT

C++的作用就是提高程序书写的效率,那么就得代码复用。

STL,即:Standard Template(样板) Library,是C++的一部分(常用的数据结构)。STL是常用数据结构和算法的集合。STL的目标是标准化组件,提高开发效率和程序可靠性。

 

STL库作为为C++的一部分与编译器一同被发布。STL主要由以下3个部分组成:

容器(Container):管理数据的集合

算法(Algorithm):处理集合内的元素

迭代器(Iterator):遍历集合内的元素

2. STL的容器

容器中存放的都必须是值而不能是引用

容器内部实施的是值拷贝操作。

容器中可以存放指针作为数据元素。

STL中的容器其实就是数据结构课程中学习的链表,栈队列和哈希表等结构。

l  线性表的典型操作

―size:获取当前容器中的元素数目

―insert:在当前元素前插入新元素

―erase:删除当前元素

―empty:判断当前容器是否为空

―front:获取第一个元素

―back:获取最后一个元素

l  vector的使用

#include <cstdlib>

#include <iostream>

#include <vector>

using namespace std;

int main(int argc, char *argv[])

{

    vector<int> vi(10);

    cout<<"vi size: "<<vi.size()<<endl;

    for(int i=0; i<5; i++)

    {

        vi[i] = i + 1;

    }

    vi.resize(5);

    cout<<"Elements in vi:"<<endl;

    for(int i=0; i<vi.size(); i++)

    {

        cout<<vi[i]<<endl;

    }

    vector<int> vin;

    vin = vi;

    vi.resize(0);

    cout<<"Elements in vin:"<<endl;

    for(int i=0; i<vin.size(); i++)

    {

        cout<<vin[i]<<endl;

    }

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

运行结果:

vi size:10

Elements in vi:

1

2

3

4

5

Elements in vin:

1

2

3

4

5

l  队列性质:

先进先出(FIFO)

 

l  栈性质:

后进先出(LIFO)

 

 

l  栈和队列的典型操作

―push:无返回值,将元素压栈(队列)

―pop:弹出栈(队列)第一个元素

―top:返回栈第一个元素

―front:返回队列第一个元素

l  stack和queue的使用

#include <cstdlib>

#include <iostream>

#include <stack>

#include <queue>

using namespace std;

void StackUsage()

{

    cout<<"Stack Usage"<<endl;

    stack<double> s;

    for(int i=0; i<5; i++)

    {

        s.push(i/100.0);

    }

    cout<<"Elements in s:"<<endl;

    while( !s.empty() ) //如果栈不为空

    {

        double v = s.top();//将栈顶元素给v

        s.pop();  //把栈顶的元素弹出来

        cout<<v<<endl;//出栈顺序与入栈顺序相反

    }

}

void QueueUsage()

{

    cout<<"Queue Usage"<<endl;

    queue<int> q;

    for(int i=0; i<5; i++)

    {

        q.push(i + 1);

    }

    cout<<"Elements in q:"<<endl;

    while( !q.empty() )

    {

        int v = q.front(); //取得队列的第一个元素

        q.pop();  //第一个元素出队列

        cout<<v<<endl;  //出队与入队的顺序是一样的

    }

}

int main(int argc, char *argv[])

{

    StackUsage();

    QueueUsage();

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

运行结果:

Stack Usage

Elements in s:

0.04

0.03

0.02

0.01

0

Queue Usage

Elements in q:

1

2

3

4

5

3. STL中的迭代器

STL中的迭代器是遍历容器的“标准”方式。

―迭代器可以理解成一个指向元素的“指针”。

 

 

l  list和iterator的使用

#include <cstdlib>

#include <iostream>

#include <list> //双向链表

using namespace std;

int main(int argc, char *argv[])

{

    list<double> l;

    cout<<"l size: "<<l.size()<<endl;

    for(int i=0; i<5; i++)

    {

        l.push_back((i + 1) / 1000.0);//在双向链表的最后插入新的东西

    }

    cout<<"l size: "<<l.size()<<endl;

    list<double>::iterator current = l.begin(); 

    /*iterator 是个迭代器*/

    cout<<"Elements in l:"<<endl;

    while( current != l.end() )

    {

        cout<<*current<<endl;   

        current++;

    }

    current = l.begin();

    current++;

    current++;

    current--;  //双向链表可以减少

    l.insert(current, 0.1);

    cout<<"Elements in l:"<<endl;

    for(list<double>::iterator p = l.begin(); p != l.end(); p++)

    {

        cout<<*p<<endl;

    }

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

运行结果:

l size:0

l size:5

Elements in l:

0.01

0.02

0.03

0.04

0.05

Elements in l:

0.01

0.1

0.02

0.03

0.04

0.05

Press the enter key to continue ...

4. STL的算法

STL中提供了大多数常用的泛型算法

如:遍历遍历,排序,反转,合并等等

算法头文件:<algorithm>

l  STL算法的使用

#include <cstdlib>

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

void current(int& v)

{

    cout<<v<<endl;

}

void print(vector<int>& vec)

{

    cout<<"Elements in vector:"<<endl; 

    for_each(vec.begin(), vec.end(), current);

}

int compare(const int& a, const int& b)

{

    return a < b;

}

int main(int argc, char *argv[])

{

    vector<int> v(10);

    for(int i=9; i>=0; i--)

    {

        v[i] = 9 - i;

    }     

    print(v); 

    sort(v.begin(), v.end(), compare); 

    print(v);

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

运行结果:

Elements in vector:

9

8

7

6

5

4

3

2

1

0

Elements in vector:

0

1

2

3

4

5

6

7

8

9

小结:

STL是是C++标配的模板库,是工程经验的总结,也是开发效率和软件可靠性的保证。

 

猜你喜欢

转载自www.cnblogs.com/free-1122/p/11336294.html