#include<algorithm>
#include<cstdio>
#include<list> // 要有这个头文件才行。
#include<iostream>
using namespace std;
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)
const int MAXN = 1e5;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
int main(){
// CLOSE();
// fread();
// fwrite();
list<int>one; // STL中自带的是双向链表
list<int>::iterator it;
list<int>::reverse_iterator itt;
one.push_front(1);
one.push_front(2);// 前插
one.push_back(3);// 后插
one.push_back(4);
one.push_back(4);
one.push_front(5);
one.push_back(6);
printf("%d\n",one.size()); // 返回链表元素个数。
for(it=one.begin();it!=one.end();it++) cout<<*it<<" "; // 正向迭代器
puts("");
for(itt=one.rbegin();itt!=one.rend();itt++) cout<<*itt<<" "; //反向迭代器
reverse(one.begin(),one.end()); // 翻转
puts("");
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts(""); one.sort(); // 排序 默认由小到大
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts(""); one.unique(); // 去重
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts(""); one.pop_back(); // 后删
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts("");one.pop_front(); //前删
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts("");
it=max_element(one.begin(),one.end()); printf("%d",*it); // 获取链表中的最大值
puts("");
one.push_back(3); one.remove(3); // 相同元素都会被删除
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
puts("") ;
it=one.begin();it++;it++;one.insert(it,5);// 向迭代器部位插入元素
for(it=one.begin();it!=one.end();it++) cout<<*it<<" ";
return 0;
}
STL <list> 常用函数
最新推荐文章于 2022-04-07 15:08:26 发布