STL容器

    技术2022-05-11  124

    这几天看了STL的部分,觉得还可以STL很实用的,以前总是怕学这个,现在真的学了,发现原来是如此的简单方便,呵呵!我这个缺点看来真的要改一改了

     test.cpp : 定义控制台应用程序的入口点。

    #include "stdafx.h"#include <iostream>#include <vector>#include <deque>#include <list>#include <queue>#include <set>#include <map>#include <bitset>#include <stack>using namespace std;//向量类模板int main(){ std::vector<char> charvector; int x = 0;

     for(int i=0; i<10;i++)  charvector.push_back(65 + i); std::vector<char>::iterator iter; for(iter=charvector.begin();iter!=charvector.end();iter++) {  cout<<"Element #"<<x++<<" : "<<*iter<<endl; } cout<<"当前数组的个数为:"<<charvector.size()<<endl;  vector<char>::iterator start = charvector.begin(); charvector.insert(start,5,'x'); cout<<"after insert :"<<endl; for(iter=charvector.begin();iter!=charvector.end();iter++)  cout<<*iter; cout<<endl;

     cout<<"after delete :"<<endl; int size=charvector.size(); for(int i=0;i<size;i++) {  vector<char>::iterator start = charvector.begin();  charvector.erase(start);  vector<char>::iterator iter1;  for(iter1=charvector.begin();iter1!=charvector.end();iter1++)   cout<<*iter1;  cout<<endl; }

     return 0;}

    //双端队列类模板

    int main(){ deque<char> chardeque; for(int i=0;i<10;++i)  chardeque.push_front(65+i); int size=chardeque.size(); deque<char>::iterator iter; for(iter=chardeque.begin();iter!=chardeque.end();iter++)  cout<<*iter; for(int i=0;i<size;i++) {  chardeque.pop_back();  for(iter=chardeque.begin();iter!=chardeque.end();iter++)   cout<<*iter;  cout<<endl; }

     return 0;}//链表类模板int main(){ list<char> charlist; int x=0; for(int i=0;i<10;i++)  charlist.push_front(65+i);

     list<char>::iterator iter; for(iter=charlist.begin();iter!=charlist.end();iter++)  cout<<*iter; cout<<endl; list<char>::iterator start=charlist.begin(); charlist.insert(++start,5,'x'); for(iter=charlist.begin();iter!=charlist.end();iter++)  cout<<*iter; cout<<endl;

     return 0;}//容器适配器stackint main(){ stack<int,list<int> > intstack; for(int x=1;x<=10;x++) {  intstack.push(x*100); } int size=intstack.size(); for(int i=0;i<size;i++) {  cout<<intstack.top()<<endl;  intstack.pop(); } return 0;}//容器适配器queueint main(){ queue<int,list<int> > intqueue; for(int x=1; x<=10; x++) {  intqueue.push(x*100); } int size = intqueue.size(); for(int i=0;i<size;i++) {  cout<<"intqueue "<<intqueue.front()<<endl;  intqueue.pop(); } return 0;}//优先级队列int  main(){ std::priority_queue<int, std::vector<int>,std::greater<int> >  intp_queue; intp_queue.push(400); intp_queue.push(100); intp_queue.push(500); intp_queue.push(300); intp_queue.push(200); int size = intp_queue.size(); for(int i=0; i<size; i++) {  cout<<" "<<intp_queue.top()<<endl;  intp_queue.pop(); } cout<<endl; return 0;}

    //集合int main(){ set<int> intset;//升序                               intset.insert(10); intset.insert(5); intset.insert(1); intset.insert(3); intset.insert(8); set<int>::iterator iter; for(iter=intset.begin();iter!=intset.end();iter++)  cout<<*iter<<" "; cout<<endl; cout<<"after delete :"<<endl; set<int>::iterator start; start=intset.begin(); intset.erase(start++); for(iter=intset.begin();iter!=intset.end();iter++)  cout<<*iter<<" "; cout<<endl; cout<<"find data in set"<<endl; iter = intset.find(5); if(iter == intset.end())  cout<<"can't find 5"<<endl; else  cout<<"ok,find the 5"<<endl; return 0;}

    //多重数据集int main(){ multiset<int> intmset;  intmset.insert(10); intmset.insert(5); intmset.insert(1); intmset.insert(3); intmset.insert(8); intmset.insert(5); intmset.insert(8); multiset<int>::iterator iter; for(iter=intmset.begin();iter!=intmset.end();iter++)  cout<<*iter<<" "; cout<<endl;    iter=intmset.begin(); intmset.erase(++iter); for(iter=intmset.begin();iter!=intmset.end();iter++)  cout<<*iter<<" "; cout<<endl;    iter=intmset.find(3); if(iter==intmset.end())  cout<<"sorry,it's error !"<<endl; else  cout<<"It's find the 3"<<endl; return 0;}

    //映射int main(){ map<int,char> charmap; charmap.insert(map<int,char>::value_type(1,'A')); charmap.insert(map<int,char>::value_type(5,'B')); charmap.insert(map<int,char>::value_type(2,'C')); charmap.insert(map<int,char>::value_type(3,'D')); charmap.insert(map<int,char>::value_type(4,'E')); map<int,char>::iterator iter; for(iter=charmap.begin();iter!=charmap.end();iter++)  cout<<(*iter).first<<"--"<<(*iter).second<<" "; cout<<endl; cout<<"after delete :"<<endl; iter = charmap.begin(); charmap.erase(++iter); for(iter=charmap.begin();iter!=charmap.end();iter++)  cout<<(*iter).first<<"--"<<(*iter).second<<" "; cout<<endl;  std::map<int ,char>::iterator i=charmap.find(4); if(i==charmap.end())  cout<<"sorry,can't find"<<endl; else  cout<<"ok,it's find"<<endl; return 0;}

    //位集合void display(std::bitset<8> bs){ cout<<"bitset = "; for(int i=0;i<8;i++)  cout<<bs[i]; cout<<endl;}int main(){ bitset<8> bitset; display(bitset); cout<<"set bits 1,2,3,5,7"<<endl; bitset.set(1); bitset.set(2); bitset.set(3); bitset.set(5); bitset.set(7); display(bitset); cout<<"reset bits 2,3"<<endl; bitset.reset(2); bitset.reset(3); display(bitset);

     for(int i=0;i<8;i++) {  cout<<"bit "<<i<<" is ";  if(bitset.test(i))//位的测试   cout<<"set"<<endl;  else   cout<<"unset"<<endl; } return 0;}


    最新回复(0)