第十四章 STL容器第三节 stack、Queue、List、priority_queue容器
stack容器
- Stack简介
- stack是堆栈容器,是一种“先进后出”的容器。
- stack是简单地装饰deque容器而成为另外的一种容器。
#include <stack>
-
使用举例:
//初始化 stack<int> stkInt; //入栈 stkInt.push(1); stkInt.push(3); stkInt.push(5); stkInt.push(7); //出栈:从栈头移除第一个元素 stkInt.pop(); //赋值 stack<int> stkIntB(stkInt); //拷贝构造 stack<int> stkIntC; stkIntC = stkInt; //赋值 //返回栈顶元素 int topint = stkInt.top(); //修改栈顶元素 stkInt.top() = 19; int topint2 = stkInt.top(); cout << topint2 << endl; //判断是否为空 bool emptyint = stkInt.empty(); //返回栈的大小 int size = stkInt.size();
Queue容器
- Queue简介
- queue是队列容器,是一种“先进先出”的容器。
- queue是简单地装饰deque容器而成为另外的一种容器。
#include <queue>
-
使用举例:
queue<int> queInt; //往队尾添加元素 queInt.push(1); queInt.push(3); queInt.push(5); queInt.push(7); queInt.push(9); //从队头移除第一个元素 queInt.pop(); queInt.pop(); //此时queInt存放的元素是5, 7, 9 //拷贝 queue<int> queIntB(queInt); //拷贝构造 queue<int> queIntC; queIntC = queInt; //赋值 //返回第一个元素 int iFront = queInt.front(); //1 //返回最后一个元素 int iBack = queInt.back(); //9 //修改 queInt.front() = 11; //11 queInt.back() = 19; //19 //判断是否为空 bool emptyint = queInt.empty(); //返回队列的大小 int size = queInt.size();
List容器
- 简介
- list是一个双向链表容器,可高效地进行插入删除元素。
- list不可以随机存取元素,所以不支持
at.(pos)
函数与[]操作符。It++(ok) it+5(err)
#include <list>
-
使用举例:
list<int> lstInt; //在容器尾部添加一个元素 lstInt.push_back(1); lstInt.push_back(3); lstInt.push_back(5); lstInt.push_back(7); lstInt.push_back(9); //删除容器第一个元素 lstInt.pop_front(); //在容器头部添加一个元素 lstInt.push_front(11); lstInt.push_front(13); //删除容器最后一个元素 lstInt.pop_back(); //返回第一个元素 int iFront = lstInt.front(); //1 //返回最后一个元素 int iBack = lstInt.back(); //9 //修改第一个、最后一个元素的值 lstInt.front() = 11; //11 lstInt.back() = 19; //19 /* list.begin(); //返回容器中第一个元素的迭代器。 list.end(); //返回容器中最后一个元素之后的迭代器。 list.rbegin(); //返回容器中倒数第一个元素的迭代器。 list.rend(); //返回容器中倒数最后一个元素的后面的迭代器。 */ //正向遍历 for (list<int>::iterator it = lstInt.begin(); it != lstInt.end(); ++it) { cout << *it; cout << " "; } //逆向遍历 for (list<int>::reverse_iterator rit = lstInt.rbegin(); rit != lstInt.rend(); ++rit) { cout << *rit; cout << " "; } //带参数构造 //构造函数将[beg, end)区间中的元素拷贝给本身。注意该区间是左闭右开的区间。 list<int> lstIntB(lstInt.begin(), lstInt.end()); //1 3 5 7 9 //构造函数将n个elem拷贝给本身。 list<int> lstIntC(5, 8); //8 8 8 8 8 //拷贝构造函数。 list<int> lstIntD(lstInt); //1 3 5 7 9 //赋值 lstIntB.assign(lstInt.begin(), lstInt.end()); //1 3 5 7 9 lstIntC.assign(5, 8); //8 8 8 8 8 lstIntD = lstInt; //1 3 5 7 9 lstIntC.swap(lstIntD); //互换 //大小 int iSize = lstInt.size(); //3 //list.resize(num); 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。 lstInt.resize(5); //1 3 5 0 0 //list.resize(num, elem); 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。 lstInt.resize(7, 1); //1 3 5 0 0 1 1 lstInt.resize(2); //1 3 //插入 /* list.insert(pos,elem); //在pos位置插入一个elem元素的拷贝,返回新数据的位置。 list.insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。 list.insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。 */ list<int> lstA; list<int> lstB; lstA.push_back(1); lstA.push_back(3); lstA.push_back(5); lstA.push_back(7); lstA.push_back(9); lstB.push_back(2); lstB.push_back(4); lstB.push_back(6); lstB.push_back(8); lstA.insert(lstA.begin(), 11); //{11, 1, 3, 5, 7, 9} lstA.insert(++lstA.begin(), 2, 33); //{11,33,33,1,3,5,7,9} lstA.insert(lstA.begin(), lstB.begin(), lstB.end()); //{2,4,6,8,11,33,33,1,3,5,7,9}
-
list的删除
/* list.clear(); //移除容器的所有数据 list.erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。 list.erase(pos); //删除pos位置的数据,返回下一个数据的位置。 lst.remove(elem); //删除容器中所有与elem值匹配的元素。 */ //删除区间内的元素 //lstInt是用list<int>声明的容器,现已包含按顺序的1,3,5,6,9元素。 list<int>::iterator itBegin=lstInt.begin(); ++ itBegin; list<int>::iterator itEnd=lstInt.begin(); ++ itEnd; ++ itEnd; ++ itEnd; lstInt.erase(itBegin,itEnd); //此时容器lstInt包含按顺序的1,6,9三个元素。 //假设 lstInt 包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素的方法一 for(list<int>::iterator it=lstInt.being(); it!=lstInt.end(); ) //小括号里不需写 ++it { if(*it == 3){ it = lstInt.erase(it); //以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。 //此时,不执行 ++it; }else{ ++it; } } //删除容器中等于3的元素的方法二 lstInt.remove(3); //删除lstInt的所有元素 lstInt.clear(); //容器为空
-
list的反序排列
lst.reverse(); //反转链表,比如lst包含1,3,5元素,运行此方法后,lst就包含5,3,1元素。
小结:
- 容器deque的使用方法
- 适合 在头尾添加移除元素。使用方法与vector类似。
- 容器queue,stack的使用方法
- 适合队列,堆栈的操作方式。
- 容器list的使用方法
- 适合在任意位置快速插入移除元素
优先级队列priority_queue
- 最大值优先级队列、最小值优先级队列
- 最大值优先级队列: 谁的值大,谁放在最前头,队列中的值由大到小排列
- 最小值优先级队列: 谁的值小,谁放在前头,队列中的值由小到大排列
- 优先级队列适配器 STL priority_queue
-
用来开发一些特殊的应用,请对stl的类库,多做扩展性学习
priority_queue<int> p1; //默认是 最大值优先级队列 //priority_queue<int, vector<int>, less<int> > p1; //相当于这样写 priority_queue<int, vector<int>, greater<int>> p2; //最小值优先级队列 p1.push(33); p1.push(11); p1.push(55); p1.push(22); cout << "队列大小" << p1.size() << endl; cout << "队头" << p1.top() << endl; while (p1.size() > 0) { cout << p1.top() << " "; p1.pop(); } cout << endl;// 55,33,22,11 cout << "测试 最小值优先级队列" << endl; p2.push(33); p2.push(11); p2.push(55); p2.push(22); while (p2.size() > 0) { cout << p2.top() << " "; p2.pop(); } //11,22,33,55 }