STL

来源:互联网 发布:thinkphp3.2案例源码 编辑:程序博客网 时间:2024/04/29 15:45

向量(vector) <vector>

[cpp] view plaincopy
  1. 连续存储的元素<vector>  
  2.   
  3. Vector<int>c;  
  4.   
  5. c.back()    传回最后一个数据,不检查这个数据是否存在。  
  6.   
  7. c.clear()     移除容器中所有数据。  
  8.   
  9. c.empty()   判断容器是否为空。  
  10.   
  11. c.front()     传回地一个数据。  
  12.   
  13. c.pop_back() 删除最后一个数据。  
  14.   
  15. c.push_back(elem)  在尾部加入一个数据。  
  16.   
  17. c[i] 等同于 c.at(i);  


列表(list) <list>

由节点组成的双向链表,每个结点包含着一个元素<list>

[cpp] view plaincopy
  1. list<int> list1(1,2,3)  
  2.   
  3. front()返回第一个元素的引用  int nRet =list1.front()    // nRet = 1  
  4.   
  5. back()返回最后一元素的引用  int nRet =list1.back()     // nRet = 3  
  6.   
  7. push_back()增加一元素到链表尾 list1.push_back(4)       //list1(1,2,3,4)  
  8.   
  9. push_front()增加一元素到链表头  list1.push_front(4)      //list1(4,1,2,3)  
  10.   
  11. pop_back()删除链表尾的一个元素 list1.pop_back()          //list1(1,2)  
  12.   
  13. pop_front()删除链表头的一元素  list1.pop_front()          //list1(2,3)  
  14.   
  15. clear()删除所有元素   list1.clear();   // list1空了,list1.size()=0  
  16.   
  17. sort()对链表排序,默认升序(可自定义回调函数)   
  18.   
  19. list对象L1(4,3,5,1,4)  L1.sort();                  
  20.   
  21.  //L1(1,3,4,4,5) L1.sort(greater<int>());   
  22.   
  23. //L1(5,4,4,3,1)  
  24.   
  25. insert()在指定位置插入一个或多个元素  
  26.   
  27. list1.insert(++list1.begin(),9);  // list1(1,9,2,3)  
  28.   
  29. list1.insert(list1.begin(),2,9);  // list1(9,9,1,2,3);  
  30.   
  31. list1.insert(list1.begin(),list2.begin(),--list2.end());//list1(4,5,1,2,3);  
  32.   
  33. swap()交换两个链表(两个重载)  
  34.   
  35. list1.swap(list2);   //list1(4,5,6) list2(1,2,3)  
  36.   
  37. unique()删除相邻重复元素  
  38.   
  39. L1(1,1,4,3,5,1)  
  40.   
  41. L1.unique();// L1(1,4,3,5,1)  
  42.   
  43. merge()合并两个有序链表并使之有序  
  44.   
  45. // 升序list1.merge(list2);          //list1(1,2,3,4,5,6) list2现为空  
  46.   
  47. // 降序L1(3,2,1), L2(6,5,4)L1.merge(L2, greater<int>()); //list1(6,5,4,3,2,1) list2现为空  
  48.   
  49. reverse()反转链表:list1.reverse();     //list1(3,2,1)  
  50.   
  51. remove()删除链表中匹配值的元素(匹配元素全部删除)list对象L1(4,3,5,1,4)L1.remove(4);               //L1(3,5,1);  
  52.   
  53. empty()判断是否链表为空bool bRet =L1.empty(); //若L1为空,bRet = true,否则bRet = false。  
  54.   
  55. rbegin()返回链表最后一元素的后向指针(reverse_iteratoror const)list<int>::reverse_iterator it = list1.rbegin();  //*it = 3  
  56.   
  57. rend()返回链表第一元素的下一位置的后向指针list<int>::reverse_iteratorit = list1.rend(); // *(--riter) = 1  


 

集合(set) <set>

由节点组成的红黑树,每个节点都包含着一个元素,节点之间以某种作用于元素对的谓词排列,没有两个不同的元素能够拥有相同的次序 <set>

[cpp] view plaincopy
  1. set<type>: 以less<>为排序法则的set  
  2.   
  3. set<type,op>: 以op为排序法则的set  
  4.   
  5. struct op{  
  6.   
  7.     bool operator()(const rec&a,const rec&b){  
  8.   
  9.         return a.x<b.x||a.x==b.x&&a.y<b.y;  
  10.   
  11.     }  
  12.   
  13. };  


1.1 set::begin

功能:返回第一个元素的定位器(iterator)的地址。

[cpp] view plaincopy
  1. set <char>::iterator cp;  
  2.   
  3. ctr.insert('a');  
  4.   
  5. ctr.insert('b');  
  6.   
  7. cp=ctr.begin(); //定位到ctr 的开始位置  


1.2 set::clear

功能:将一个set 容器的全部元素删除。

1.3 set::count

功能:返回对应某个关键字的元素的个数。好像都是1吧

1.4 set::empty

功能:测试一个set 容器是否为空。

1.5 set::end

功能:返回最后一个元素后面的定位器(iterator)的地址。

1.7 set::erase

功能:将一个或一定范围的元素删除。

1.8 set::find

功能:求出与给定的关键字相等的元素的定位器。

[cpp] view plaincopy
  1. set <string> ctr;  
  2.   
  3.     ctr.insert("abc");  
  4.   
  5.     ctr.insert("abcd");  
  6.   
  7.     ctr.insert("abcf");  
  8.   
  9.     set <string>::iterator cp;  
  10.   
  11.     cp=ctr.find("abc"); //查找key=1 的元素  
  12.   
  13.     if(cp!=ctr.end())  
  14.   
  15.         cout<<*cp <<endl;//显示abc  
  16.   
  17.     cp=ctr.find("adf"); //查找key=2 的元素  
  18.   
  19.     if(cp!=ctr.end())  
  20.   
  21.         cout<<*cp <<endl;//不显示  
  22.   
  23.     cp=ctr.find("gfv"); //查找key=3 的元素  
  24.   
  25.     if(cp!=ctr.end())  
  26.   
  27.         cout<<*cp <<endl;// 不显示  


1.10 set::insert

功能:将一个元素或者一定数量的元素插入到set 的特定位置中。

1.25 set::upper_bound

功能:求出指向第一个关键字的值是大于一个给定值的元素的定位器。

[cpp] view plaincopy
  1. cp=ctr.upper_bound(2);//输出比2大的最小元素  


多重集合(multiset)< set>

允许存在两个次序相等的元素的集合 <set>

[cpp] view plaincopy
  1. multiset<type>: 以less<>为排序法则的multiset  
  2.   
  3. multiset<type, op>: 以op为排序法则的multise  
  4.   
  5. struct op{  
  6.   
  7. bool operator()(const rec&a,const rec&b){  
  8.   
  9.         return a.x<b.x||a.x==b.x&&a.y<b.y;  
  10.   
  11.     }  
  12.   
  13. };  
  14.   
  15. multiset<int>h;  
  16.   
  17. __typeof(h.begin()) c=h.begin();//c指向h序列中第一个元素的地址,第一个元素是最小的元素  
  18.   
  19. printf("%d ",*c);//将地址c存的数据输出  
  20.   
  21. h.erase(c);//从h序列中将c指向的元素删除  
  22.   
  23. __typeof()是个好东西~  


栈(stack)  <stack>

后进先出的值的排列 <stack>

定义一个stack的变量stack<int> s;

入栈,如例:s.push(x);

出栈,如例:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。

访问栈顶,如例:s.top()

判断栈空,如例:s.empty(),当栈空时,返回true。

访问栈中的元素个数,如例:s.size()

队列(queue) <queue>

先进先出的执的排列 <queue>

定义一个queue的变量     queue<Type> M
查看是否为空范例        M.empty()   是的话返回1,不是返回0;
从已有元素后面增加元素M.push()
输出现有元素的个数        M.size()
显示第一个元素              M.front()
显示最后一个元素           M.back()
清除第一个元素              M.pop()

优先队列(priority_queue) <queue>

元素的次序是由作用于所存储的值对上的某种谓词决定的的一种队列 <queue>

1、默认从大到小

priority_queue<int> qi;

2、从小到大输出可以传入一个比较函数,使用functional.h函数对象作为比较函数,great<int>(小到大) less<int>(大到小)

priority_queue<int, vector<int>, greater<int> >qi2; 第二个参数为容器类型。第三个参数为比较函数。

3、自定义:

[cpp] view plaincopy
  1. struct cmp  // 最小优先队列  
  2.   
  3. {  
  4.   
  5.     bool operator()(const long long i,constlong long j)  
  6.   
  7.     {  
  8.   
  9.         return i>j;  
  10.   
  11.     }  
  12.   
  13. };  
  14.   
  15. priority_queue<int,vector<longlong>,cmp> Q;  
  16.   
  17.    
  18.   
  19.    
  20.   
  21. struct node // 最小优先队列  
  22.   
  23. {  
  24.   
  25.     int id,len;  
  26.   
  27.     bool operator < (const node &b)const// 只能重载小于号  
  28.   
  29.     {  
  30.   
  31.         return len>b.len;  
  32.   
  33.     }  
  34.   
  35. };  
  36.   
  37. priority_queue<node>Q;  
  38.   
  39.    
  40.   
  41. Q.empty() // 判断队列是否为空返回ture表示空返回false表示空 bool  
  42.   
  43. Q.top() // 返回顶端元素的值元素还在队列里  
  44.   
  45. Q.pop() // 删除顶端元素 void  
  46.   
  47. Q.push(V) // 把long long型的数V加入到队列里它会制动条件V的位置void  
  48.   
  49. Q.size() // 返回队列里元素个数 unsigned int  


双端队列(deque) <deque>

连续存储的指向不同元素的指针所组成的数组<deque>

[cpp] view plaincopy
  1. deque<int>c  
  2.   
  3. c.pop_back()      删除最后一个数据。  
  4.   
  5. c.pop_front()      删除头部数据。  
  6.   
  7. c.push_back(elem) 在尾部加入一个数据。  
  8.   
  9. c.push_front(elem) 在头部插入一个数据。  
  10.   
  11. c.clear()          移除容器中所有数据。  
  12.   
  13. c.front()          传回地一个数据。  
  14.   
  15. c.back()          传回最后一个数据,不检查这个数据是否存在。  
  16.   
  17. c.size()           返回容器中实际数据的个数。  
  18.   
  19. c.empty()         判断容器是否为空。  
  20.   
  21. c[i] 等同于 c.at(i);  


 

 

映射(map) 由{键,值}对组成的集合 <map>

以某种作用于键对上的谓词排列 <map>

三种插入数据的方法(第一种和第二种是一样的,当map中有这个关键字时,insert操作是插入不了数据的,用数组方式会覆盖以前该关键字对应的值)

[cpp] view plaincopy
  1. 1. map<int,string>mapStudent;  
  2.   
  3.    mapStudent.insert(pair<int,string>(1,"student_one"));  
  4.   
  5. mapStudent.insert(pair<int,string>(2,"student_two"));  
  6.   
  7. 2. map<int,string>mapStudent;  
  8.   
  9.    mapStudent.insert(map<int,string>::value_type(1,"student_one"));  
  10.   
  11.    mapStudent.insert(map<int,string>::value_type(2,"student_two"));  
  12.   
  13. 3. map<int,string>mapStudent;  
  14.   
  15. mapStudent[1]="student_one";  
  16.   
  17. mapStudent[2]="student_two";  
  18.   
  19. 可以用pair来获得是否插入成功  
  20.   
  21. map<int,string>mapStudent;  
  22.   
  23. pair<map<int,string>::iterator,bool> insert_pair;  
  24.   
  25. insert_pair=mapStudent.insert(pair<int,string>(1,"student_one"));  
  26.   
  27. if(insert_pair.second==true)      
  28.   
  29.      cout<<"insert Successfully"<<endl;  


怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:int nSize=mapStudent.size();

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数

要判定一个数据(关键字)是否在map中出现的方法比较多,这里给出三种数据查找方法

第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,返回1

   

[cpp] view plaincopy
  1. map<int,string> mapStudent;  
  2.   
  3. mapStudent.insert(pair<int,string>(1,"student_one"));  
  4.   
  5. mapStudent.insert(pair<int,string>(2,"student_two"));  
  6.   
  7. mapStudent.insert(pair<int,string>(3,"student_three"));  
  8.   
  9. int t1,t2;  
  10.   
  11. t1=mapStudent.count(4);  
  12.   
  13. t2=mapStudent.count(1);  


第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器

      

[cpp] view plaincopy
  1.   map<string,int>mapStudent;  
  2.   
  3.          mapStudent.insert(pair<string,int>("student_one",1));  
  4.   
  5.          mapStudent.insert(pair<string,int>("student_two",2));  
  6.   
  7.          mapStudent.insert(pair<string,int>("student_three",3));  
  8.   
  9.          map<string,int>::iteratoriter;  
  10.   
  11.          charch[]="student_three";  
  12.   
  13.          iter=mapStudent.find(ch);  
  14.   
  15.          if(iter!=mapStudent.end())  
  16.   
  17.                    cout<<"Find,the value is: "<<iter->second<<endl;  
  18.   
  19.          else  
  20.   
  21.                    cout<<"Donot Find"<<endl;  
  22.   
  23. 清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空  
  24.   
  25. //如果要删除,用迭代器删除  
  26.   
  27. map<int,string>::iterator iter;  
  28.   
  29. iter=mapStudent.find(1);  
  30.   
  31. mapStudent.erase(iter);  
  32.   
  33. //如果要删除,用关键字删除  
  34.   
  35. intn=mapStudent.erase(1);//如果删除了n会返回,否则返回  
  36.   
  37. //用迭代器,成片的删除  
  38.   
  39. mapStudent.erase(mapStudent.begin(),mapStudent.end());  
  40.   
  41. //一下代码把整个map清空  
  42.   
  43. mapStudent.erase(mapStudent.begin(),mapStudent.end());  


排序

一、

[cpp] view plaincopy
  1. #include <map>  
  2.   
  3. #include <string>  
  4.   
  5. using namespace std;  
  6.   
  7. typedef struct tagStudentInfo  
  8.   
  9. {  
  10.   
  11.          int      nID;  
  12.   
  13.          string   strName;  
  14.   
  15.          booloperator < (tagStudentInfo const& _A) const  
  16.   
  17.          {  
  18.   
  19.                    //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序  
  20.   
  21.                    if(nID< _A.nID)  
  22.   
  23.            return true;  
  24.   
  25.                    if(nID== _A.nID)  
  26.   
  27.            return strName.compare(_A.strName) < 0;  
  28.   
  29.                    returnfalse;  
  30.   
  31.          }  
  32.   
  33. }StudentInfo, *PStudentInfo;  //学生信息  
  34.   
  35.    
  36.   
  37.    
  38.   
  39. int main()  
  40.   
  41. {  
  42.   
  43.    //用学生信息映射分数  
  44.   
  45.    map<StudentInfo, int>mapStudent;  
  46.   
  47.    StudentInfo studentInfo;  
  48.   
  49.    
  50.   
  51.    studentInfo.nID = 1;  
  52.   
  53.    studentInfo.strName ="student_one";  
  54.   
  55.    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  56.   
  57.    
  58.   
  59.    studentInfo.nID = 2;  
  60.   
  61.    studentInfo.strName ="student_two";  
  62.   
  63.    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  64.   
  65. }  


二、

[cpp] view plaincopy
  1. #include <map>  
  2.   
  3. #include <string>  
  4.   
  5. #include <iostream>  
  6.   
  7. using namespace std;  
  8.   
  9. typedef struct tagStudentInfo  
  10.   
  11. {  
  12.   
  13.    int      nID;  
  14.   
  15.    string   strName;  
  16.   
  17. } StudentInfo, *PStudentInfo; //学生信息  
  18.   
  19.    
  20.   
  21. struct sort  
  22.   
  23. {  
  24.   
  25.    bool operator() (StudentInfo const &_A, StudentInfo const &_B)const  
  26.   
  27.     {  
  28.   
  29.        if(_A.nID < _B.nID)  
  30.   
  31.            return true;  
  32.   
  33.        if(_A.nID == _B.nID)  
  34.   
  35.            return _A.strName.compare(_B.strName) < 0;  
  36.   
  37.        return false;  
  38.   
  39.     }  
  40.   
  41. };  
  42.   
  43.    
  44.   
  45. int main()  
  46.   
  47. {  
  48.   
  49.    //用学生信息映射分数  
  50.   
  51.    map<StudentInfo, int, sort>mapStudent;  
  52.   
  53.    StudentInfo studentInfo;  
  54.   
  55.    studentInfo.nID = 1;  
  56.   
  57.    studentInfo.strName = "student_one";  
  58.   
  59.    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  60.   
  61.    studentInfo.nID = 2;  
  62.   
  63.    studentInfo.strName = "student_two";  
  64.   
  65.    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  66.   
  67.    map<StudentInfo, int>::reverse_iterator iter;  
  68.   
  69.    for(iter=mapStudent.rbegin();iter!= mapStudent.rend();iter++)  
  70.   
  71.                    cout<<iter->second<<endl;  
  72.   
  73. }  


 

多重映射(multimap)  <map>

允许键对有相等的次序的映射 <map>

比如在电话簿中相同的人可以有两个以上电话号码,文件系统中可以将多个符号链接映射到相同的物理文件,或DNS服务器可以将几个URLs映射到相同的IP地址。

查找

1. 直接找到每种键值的所有元素的第一个元素的游标。

通过函数:lower_bound( const keytype& x ), upper_bound( const keytype&x ) 可以找到比指定键值x的小的键值的第一个元素和比指定键值x大的键值的第一个元素。返回值为该元素的游标。

细节:当到达键值x已经是最大时,upper_bound返回的是这个multimap的end游标。同理,当键值x已经是最小了,lower_bound返回的是这个multimap的begin游标。

2. 指定某个键值,进行遍历

可以使用上面的lower_bound和upper_bound函数进行游历,也可以使用函数equal_range。其返回的是一个游标对。游标对pair::first是由函数lower_bound得到的x的前一个值,游标对pair::second的值是由函数upper_bound得到的x的后一个值。

[cpp] view plaincopy
  1.  multimap<int,int>a;  
  2.   
  3. a.insert(pair<int,int>(1,11));  
  4.   
  5. a.insert(pair<int,int>(1,12));  
  6.   
  7. a.insert(pair<int,int>(1,13));  
  8.   
  9. a.insert(pair<int,int>(2,21));  
  10.   
  11. a.insert(pair<int,int>(2,22));  
  12.   
  13. a.insert(pair<int,int>(3,31));  
  14.   
  15. a.insert(pair<int,int>(3,32));  
  16.   
  17.    
  18.   
  19. multimap<int,int>::iterator p_map;  
  20.   
  21. pair<multimap<int,int>::iterator,multimap<int,int>::iterator> ret;  
  22.   
  23. for(p_map = a.begin() ; p_map != a.end();)  
  24.   
  25. {  
  26.   
  27.    cout<<p_map->first<<" =>";  
  28.   
  29.    ret = a.equal_range(p_map->first);  
  30.   
  31.     for(p_map= ret.first; p_map != ret.second; ++p_map)  
  32.   
  33.        cout<<" "<< (*p_map).second;  
  34.   
  35.    cout<<endl;  
  36.   
  37. }  
0 0