STL中的新容器及功能
新一届的NOIP,允许使用STL,给编程者提供了很多方便。
STL = Standard Template Library,标准模板库,从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其他一些组件的集合。
那么我就给大家介绍介绍
先从vector讲起,vector是一种非常普遍,广受人们好评和利用的一种容器,类似于栈,可以将一个元素加入队尾,或把队尾的元素排出,其中中的元素可以进行访问,vector也可以当数组来用所以非常方便。

int main() { int i, x, b[10] = {10, 9, 2, 8, 3, 7, 4, 6, 5, 0}; vector < int > a(b, b + 10); /*直接把数组b中的元素搬过来十分方便 新定义一个的话可以这么写: vector < int > a;就行了 后面也可以加个括号, 限制vector的元素个数范围 vector < int > a(100); */ a.push_back(100);//在a尾加入一个100 vector < int > :: iterator ti, it = find(a.begin(), a.end(), 100);//迭代器当指针 , 若找不到就在end,end是虚的指针 cout << *it << endl;//找到第一个100后输出100 a.insert(it, 5, 3);//在100这个位置前面插入5个3 for (i = 0; i < a.size(); i++) cout << a[i] << ' '; //这时候体现vetor能当数组用,输出结果 10 9 2 8 3 7 4 6 5 0 3 3 3 3 3 100 cout << endl; reverse(a.begin() + 5, a.end() - 1);//把这两个地址及之间的元素调换 cout << count(a.begin(), a.end(), 3) << endl;//寻找3的个数,复杂度O(n),结果为6 a.erase(a.begin() + 2, a.end() - 4);//把这两个地址及之间的元素删除 for (it = a.begin(); it < a.end(); it++) cout << *it << ' ';//用指针来访问,输出结果 10 9 6 4 7 100 system("pause"); it = max_element(a.begin(), a.end());//找最大值的地址 ti = min_element(a.begin(), a.end());//找最小值的地址 cout << *it << ' ' << *ti << endl;//结果为 100 4 sort(a.begin(), a.end());//排序 cout << *it << ' ' << *ti << endl;//因为记住的只是位置,数改变了指针不随之改变,故结果为100 9 cin >> x; it = lower_bound(a.begin(), a.end(), x);// 二分查找 >=x cout << *it << endl; it = upper_bound(a.begin(), a.end(), x); // >x upper >= lower cout << *it <<endl; //若输入6则为6,7 若输入5则为6,6 cout << a.empty() << endl;//返回是否为空 0 cout << a.size() << endl;//返回元素个数 6 cout << a.front() << endl;//返回第一个元素 4 cout << a.back() << endl; //返回最后一个元素 100 return 0; }
然后是队列queue,queue能排掉头元素,加入新的尾元素,标准队列,不能对中间的元素进行访问,也不能当做数组,中间的都是private
queue <int> q;//定义一个q
q.pop//排掉队头
q.push(x);//队尾加入一个x元素
剩下的都一样了
队列里还有一种特殊的队列叫做优先队列,俗话说就是堆,同样是private,同样不能做数组,但是能够帮你自动排序,默认从小到大排序

#include<iostream> #include<queue> using namespace std; int main() { priority_queue<int, vector<int>,greater<int> > h;//vector做容器,比小。具体为什么要用vector容器我现在也没弄懂= = priority_queue<int>l; int n,i,t; cin>>n; for(i=1;i<=n;i++) { cin>>t; h.push(t); l.push(t); } while(!h.empty()) {cout<<h.size()<<' '<<h.top()<<" "<<l.top()<<endl; h.pop();// 删除头 l.pop(); } system("pause"); return 0; }
还有一种容器是map,十分强大,排序二叉树和查询二叉树的合体,缺点是元素不能重复,所以做图论也不是很方便。map有第一关键字和第二关键字,按第一关键字排序。
下面是一段用map写的noip2007提高组第一题count

//最慢数据9用时:输入并排序完0.36",输出完0.41" //下标是第一关键字,值是第二关键字 #include<iostream> #include<map> using namespace std; //ifstream cin("count.in"); //ofstream cout("count.out"); int main() { int x,t=clock(),n,i; map<int,int> a; cin>>n; for (i=0;i<n;i++) { cin>>x; a[x]++; } cout<<(clock()-t)/1000.0<<endl; map<int,int>::iterator it; for (it=a.begin();it!=a.end();it++) cout<<it->first<<" "<<(*it).second<<endl; cout<<(clock()-t)/1000.0<<endl; cout << a.begin() -> first << ' ' << a.end()-> first; system("pause"); return 0; }
总结一下
stack标准栈,queue标准数列,不能当数组访问
stack a;
a.top();访问栈顶
a.pop();弹出栈顶
a.push(x)
a.size();
a.empty()
queue q;
q.pop();
弹掉头 q.front();
q.back();
q.size();
q.empty();
q.push(x); 加入尾
vector v;
v.front();值
v.back();值
v.begin();迭代器,地址
v.end(); 同上
v.push_back();
v.pop_back();
v.insert( , );
v.erase( , )
sort(,)
reverse(,)
max_element(,)
min_element(,)
count(, ,)
find
upper_bound
lower_bound
vector < int > :: iterator it;
deque能处理头的vector 多d.push_front(); d.pop_front();
pair < int , int > p;
p.first();
p.second();
make_pair(x, y);
返回值是一个pair push(make.....)
pair <pair, int> 三关键字
最后想看更详细的内容请访问c++官网,搜索STL