作者:蛮小蛮将军_415 | 来源:互联网 | 2023-10-10 11:29
queue(先进先出) queue有入队push(插入)、出队pop(删除)、读取队首元素front、读取队尾元素back、empty,size这几种方法
#include
#include
using namespace std;
int main(void)
{queue<int> q;
q.push(1);q.push(2);q.push(3);q.push(4);
cout<<"队首元素"<<endl;cout<<"queue.front "<<q.front()<<endl; cout<<endl<<endl;
cout<<"队尾元素"<<endl;cout<<"queue.back "<<q.back()<<endl;
cout<<"遍历"<<endl; int sz&#61;q.size();for(int i&#61;0;i<sz;i&#43;&#43;){int temp&#61;q.front();q.pop();q.push(temp);cout<<temp<<endl; }
cout<<"当前队首出队"<<endl;q.pop();sz&#61;q.size();for(int i&#61;0;i<sz;i&#43;&#43;){int temp&#61;q.front();q.pop();q.push(temp);cout<<temp<<endl; } cout<<endl<<endl;
cout<<"查询元素3"<<endl;sz&#61;q.size(); int flag&#61;1;for(int i&#61;0;i<sz;i&#43;&#43;){int temp&#61;q.front();q.pop();q.push(temp);if(temp&#61;&#61;3&&flag){flag&#61;0;} } if(!flag){cout<<"find it"<<endl;}else{cout<<"no"<<endl;}cout<<endl<<endl;
if(q.size()&#61;&#61;0){cout<<"way:queue.size() "<<"empty"<<endl;}else{cout<<"way:queue.size() "<<"nonempty"<<endl;}if(q.empty()){cout<<"way:queue.empty() "<<"empty"<<endl;}else{cout<<"way:queue.empty() "<<"nonempty"<<endl;}cout<<endl<<endl; return 0;
}
stack&#xff08;后进先出&#xff09;
Stack(栈)是一种后进先出的数据结构&#xff0c;也就是LIFO(last in first out) &#xff0c;
最后加入栈的元素将最先被取出来&#xff0c;在栈的同一端进行数据的插入与取出&#xff0c;这一段叫做“栈顶”。使用STL的stack需要include一个头文件
#include
#include
using namespace std;int main()
{stack<int> s;s.push(1); s.push(2);s.push(4);s.push(5);cout << s.size() << endl;while(s.empty() !&#61; true){cout << s.top() << endl;s.pop();}return 0;}
list
ist是双向循环链表&#xff0c;&#xff0c;每一个元素都知道前面一个元素和后面一个元素。
在STL中&#xff0c;list和vector一样&#xff0c;是两个常被使用的容器。
和vector不一样的是&#xff0c;list不支持对元素的任意存取 。
list中提供的成员函数与vector类似&#xff0c;不过list提供对表首元素的操作push_front、pop_front&#xff0c;这是vector不具备的。
声明&#xff1a; list l
插入&#xff1a;push_back尾部&#xff0c;push_front头部&#xff0c;insert方法前往迭代器位置处插入元素&#xff0c;链表自动扩张&#xff0c;迭代器只能使用&#43;±-操作&#xff0c;不能用&#43;n -n&#xff0c;因为元素不是物理相连的。
遍历&#xff1a;iterator和reverse_iterator正反遍历
删除&#xff1a;pop_front删除链表首元素&#xff1b;pop_back()删除链表尾部元素&#xff1b;erase&#xff08;迭代器&#xff09;删除迭代器位置的元素&#xff0c;注意只能使用&#43;±-到达想删除的位置&#xff1b;remove&#xff08;key&#xff09; 删除链表中所有key的元素&#xff0c;clear&#xff08;&#xff09;清空链表。
查找&#xff1a;it &#61; find(l.begin(),l.end(),key)
排序&#xff1a;l.sort()
删除连续重复元素:l.unique() 【2 8 1 1 1 5 1】 --> 【 2 8 1 5 1】