zoukankan      html  css  js  c++  java
  • STL整理

    sort
    升序排列:

     

    iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。

     

    iterator upper_bound( const key_type &key ): 返回一个迭代器,指向键值 第一个>key的元素。

     

    降序排列:

     

    iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值<= key的第一个元素。

     

    iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值<key的最后一个元素的后一个元素。

    pair

    #include<cstdio>
    #include<utility>
    #include<map>
    #include<iostream>
    using namespace std;
    /*struct pair{
        int first;
        int second;
    }x;*///<==>
    pair<int,int>x(1,2);
    typedef pair<string, string> author;
    author pro("May", "Lily");
    author joye;
    int main(){
        printf("%d
    %d
    ",x.first,x.second); 
        
        cout<<pro.first<<endl<<joye.first<<endl;
        
        int a = 8;
        string m = "James";
        pair<int, string> newone;
        newone = make_pair(a, m);//make_pair(),返回一个pair类型
        cout<<newone.first<<endl<<newone.second<<endl;
        
        if (pro.first == "May" && pro.second == "Lily")
        cout<<"yes"<<endl;
    
        return 0;
    }

    map

     

    1. map最基本的构造函数;
       map<string , int >mapstring;         map<int ,string >mapint;
       map<sring, char>mapstring;         map< char ,string>mapchar;
       map<char ,int>mapchar;            map<int ,char >mapint;

    2. map添加数据;

       map<int ,string> maplive;  
       1.maplive.insert(pair<int,string>(102,"aclive"));
       2.maplive.insert(map<int,string>::value_type(321,"hai"));
       3, maplive[112]="April";//map中最简单最常用的插入添加!
    3,map中元素的查找:

       find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。        

       map<int ,string >::iterator l_it;; 
       l_it=maplive.find(112);
       if(l_it==maplive.end())
                    cout<<"we do not find 112"<<endl;
       else cout<<"wo find 112"<<endl;
    4,map中元素的删除:
       如果删除112;
       map<int ,string >::iterator l_it;;
       l_it=maplive.find(112);
       if(l_it==maplive.end())
            cout<<"we do not find 112"<<endl;
       else  maplive.erase(l_it);  //delete 112;
    5,map中 swap的用法:
      Map中的swap不是一个容器中的元素交换,而是两个容器交换;

    #include<map>
    #include<iostream>
    using namespace std;
    /*
      map<int,int> 下表可正可负,不会爆内存(二叉树储存原理) 
    */
    int main(){
          map <int, int> m1, m2, m3;
          map <int, int>::iterator m1_Iter;
    
          m1.insert ( pair <int, int>  ( 1, 10 ) );
          m1.insert ( pair <int, int>  ( 2, 20 ) );
          m1.insert ( pair <int, int>  ( 3, 30 ) );
          m2.insert ( pair <int, int>  ( 10, 100 ) );
          m2.insert ( pair <int, int>  ( 20, 200 ) );
          m3.insert ( pair <int, int>  ( 30, 300 ) );
    
       cout<<"The original map m1 is:";
       for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
          cout<<" "<<m1_Iter->second;
          cout<<"."<<endl;
    
       // This is the member function version of swap
       //m2 is said to be the argument map; m1 the target map
       m1.swap( m2 );
    
       cout<<"After swapping with m2, map m1 is:";
       for( m1_Iter = m1.begin( ); m1_Iter != m1.end( );m1_Iter++ )
          cout<<" "<<m1_Iter -> second;
          cout<<"."<<endl;
       cout<<"After swapping with m2, map m2 is:";
       for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
          cout<<" "<<m1_Iter -> second;
          cout<<"."<<endl;
       // This is the specialized template version of swap
       swap( m1, m3 );
    
       cout<<"After swapping with m3, map m1 is:";
       for( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
          cout<<" "<<m1_Iter -> second;
          cout<<"."<<endl;
        return 0;
    }

    6.map的sort问题:
      Map中的元素是自动按key升序排序,所以不能对map用sort函数:
      For example:

    #include<map>
    #include<cstdio>
    #include<iostream>
    using namespace std;
    int main(){
        map<int,int>test;
        map<int,int>::iterator test_Iter;
        test.insert(pair<int,int>(1,21)); 
        test.insert(pair<int,int>(1,19));
        test.insert(pair<int,int>(2,21));
        test.insert(pair<int,int>(9,51));
        test.insert(pair<int,int>(5,51));
        test.insert(pair<int,int>(4,81));
        test.insert(pair<int,int>(3,21));
        for(test_Iter=test.begin();test_Iter!=test.end();test_Iter++)
           printf("%d %d
    ",test_Iter->first,test_Iter->second);
        //自动按关键字排序 
        return 0;
    }

    7,   map的基本操作函数:
          C++ Maps是一种关联式容器,包含“关键字/值”对
          begin()          返回指向map头部的迭代器
          clear()         删除所有元素
          count()          返回指定元素出现的次数
          empty()          如果map为空则返回true
          end()            返回指向map末尾的迭代器
          equal_range()    返回特殊条目的迭代器对
          erase()          删除一个元素
          find()           查找一个元素
          get_allocator()  返回map的配置器
          insert()         插入元素
          key_comp()       返回比较元素key的函数
          lower_bound()    返回键值>=给定元素的第一个位置
          max_size()       返回可以容纳的最大元素个数
          rbegin()         返回一个指向map尾部的逆向迭代器
          rend()           返回一个指向map头部的逆向迭代器
          size()           返回map中元素的个数
          swap()            交换两个map
          upper_bound()     返回键值>给定元素的第一个位置
          value_comp()      返回比较元素value的函数

    set

    set介绍

    C++ 集合(set)是一种包含已排序对象的关联容器,能够自动对进入集合的元素排序,并且不允许重复(另一类集合(multiset)提供对重复元素的支持)。集合中的元素可以是常见类型,也可以是自定义的接口提类型。set容器有2个主要特征:

    (1)不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,再插入新元素。

    (2)不提供直接存取元素的任何操作函数,只能通过迭代器(iterator)进行间接存取,可以将迭代器类比成C里的指针。

    C++标准模板库提供通用的set模板,需要添加头文件#include<set>

    常用操作

    1.插入:insert()

    2.中序遍历:

    set<int> s;
    
    ......
    
    set<int>::iterator it; //定义迭代器 
        //中序遍历集合中的所有元素  
        for(it=s.begin();it!=s.end();it++) 
    		......
    


    3.删除:

    set<int> s;

    s.erase(2);        //删除键值为2的元素
    s.clear();           //清除所有元素

    4.元素检索:

    #include<set>
    #include<iostream>
    #include<cstdio>
    using namespace std;
    set<int>s;
    int main(){
        set<int>::iterator it;
        s.insert(2);
        it=s.find(2);
        printf("%s",it!=s.end()?"yes":"no");//yes 找到 
    }

    一个不包含重复元素的 collection。更确切地讲,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素。正如其名称所暗示的,此接口模仿了数学上的 set 抽象。
      在所有构造方法以及 add、equals 和 hashCode 方法的协定上,Set 接口还加入了其他规定,这些规定超出了从 Collection 接口所继承的内容。出于方便考虑,它还包括了其他继承方法的声明(这些声明的规范已经专门针对 Set 接口进行了修改,但是没有包含任何其他的规定)。对这些构造方法的其他规定是(不要奇怪),所有构造方法必须创建一个不包含重复元素的 set(正如上面所定义的)。注:如果将可变对象用作 set 元素,那么必须极其小心。如果对象是 set 中某个元素,以一种影响 equals 比较的方式改变对象的值,那么 set 的行为就是不确定的。此项禁止的一个特殊情况是不允许某个 set 包含其自身作为元素。
    常用的几个方法
      boolean add(E e)
      如果 set 中尚未存在指定的元素,则添加此元素(可选操作)。
      boolean addAll(Collectionc)
      如果 set 中没有指定 collection 中的所有元素,则将其添加到此 set 中(可选操作)。
      void clear()
      移除此 set 中的所有元素(可选操作)。
      boolean contains(Object o)
      如果 set 包含指定的元素,则返回 true。
      boolean containsAll(Collectionc)
      如果此 set 包含指定 collection 的所有元素,则返回 true。
      boolean equals(Object o)
      比较指定对象与此 set 的相等性。
      int hashCode()
      返回 set 的哈希码值。
      boolean isEmpty()
      如果 set 不包含元素,则返回 true。
      Iterator iterator()
      返回在此 set 中的元素上进行迭代的迭代器。
      boolean remove(Object o)
      如果 set 中存在指定的元素,则将其移除(可选操作)。
      boolean removeAll(Collectionc)
      移除 set 中那些包含在指定 collection 中的元素(可选操作)。
      boolean retainAll(Collectionc)
      仅保留 set 中那些包含在指定 collection 中的元素(可选操作)。
      int size()
      返回 set 中的元素数(其容量)。
      Object[] toArray()
      返回一个包含 set 中所有元素的数组。
      T[]
      toArray(T[] a)

      返回一个包含此 set 中所有元素的数组;返回数组的运行时类型是指定数组的类型。

    vector

    #include<vector>
    #include<cstdio>
    #include<iostream>
    #include<functional>
    #include<algorithm>
    using namespace std;
    vector<int>vec;
    int main(){
        vec.push_back(2);
        vec.push_back(1);
        vec.push_back(10);
        vec.push_back(20);
        vec.push_back(12);
        vec.push_back(182);
        vec.push_back(2);
        vec.push_back(3);
        vec.push_back(2);
        vec.push_back(3);
        sort(vec.begin(),vec.end(),greater<int>());
        for(int i=0;i<vec.size();i++){
            printf("%d ",vec[i]);
        }
        puts("");
        vec.erase(unique(vec.begin(),vec.end()),vec.end());
        sort(vec.begin(),vec.end(),less<int>());
        for(int i=0;i<vec.size();i++){
            printf("%d ",vec[i]);
        }
        return 0;
        /*
        升序:sort(begin,end,less<data-type>());
       降序:sort(begin,end,greater<data-type>()).
    */
    }

      以下整理自《ACM程序设计》  

      迭代器(iterator)

      个人理解就是把所有和迭代有关的东西给抽象出来的,不管是数组的下标,指针,for里面的、list里面的、vector里面的,抽象一下变成了iterator

    View Code
     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     vector<int> v;
     9     for(int i = 0; i < 10; ++i )
    10     {
    11         v.push_back(i);
    12     }
    13     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    14     {
    15         cout << *it << " ";
    16     }
    17     cout << endl;
    18     return 0;
    19 }

      

      求和(<numeric> accumulate)

      accumulate(v.begin(),v.end(),0),把从 v.begin() 开始到 v.end()结束所有的元素加到 0上面去

    View Code
     1 #include <iostream>
     2 #include <vector>
     3 #include <numeric>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     vector<int> v;
    10     for(int i = 0; i < 10; ++i )
    11     {
    12         v.push_back(i);
    13     }
    14     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    15     {
    16         cout << *it << " ";
    17     }
    18     cout << endl;
    19     cout << accumulate(v.begin(),v.end(),0) << endl;
    20     return 0;
    21 }

      vector(动态数组)

      vector有内存管理的机制,也就是说对于插入和删除,vector可以动态调整所占用的内存空间。  

      vector相关函数

    View Code
     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     vector<int> v;
     9     v.push_back(3);  //数组尾部插入3
    10     v.push_back(2);
    11     v.push_back(1);
    12     v.push_back(0);
    13     cout << " 下标 " << v[3] << endl;
    14     cout << " 迭代器 " << endl;
    15     for(vector<int>::iterator i = v.begin();i!= v.end();++i)
    16     {
    17         cout << *i << " ";
    18     }
    19     cout << endl;
    20     //在第一个元素之前插入111  insert begin+n是在第n个元素之前插入
    21     v.insert(v.begin(),111);
    22     //在最后一个元素之后插入222 insert end + n 是在n个元素之后插入
    23     v.insert(v.end(),222);
    24 
    25     for(vector<int>::iterator i = v.begin();i!= v.end();++i)
    26     {
    27         cout << *i << " ";
    28     }
    29     cout << endl;
    30 
    31     vector<int> arr(10);
    32     for(int i = 0; i < 10; i++)
    33     {
    34         arr[i] = i;
    35     }
    36     for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
    37     {
    38         cout << *i << " ";
    39     }
    40     cout << endl;
    41 
    42     //删除 同insert
    43     arr.erase(arr.begin());
    44 
    45     for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
    46      {
    47         cout << *i << " " ;
    48      }
    49     cout << endl ;
    50 
    51     arr.erase(arr.begin(),arr.begin()+5);
    52 
    53     for(vector<int>::iterator i = arr.begin();i!= arr.end();++i)
    54     {
    55         cout << *i << " " ;
    56     }
    57     cout << endl ;
    58     return 0 ;
    59  }


      数组转置 (<algorithm> reverse)

      reverse(v.begin(),v.end())

    View Code
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     vector<int> v;
    10     for(int i = 0; i < 10; ++i)
    11     {
    12         v.push_back(i);
    13     }
    14     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    15     {
    16         cout << *it << " ";
    17     }
    18     cout << endl;
    19 
    20     reverse(v.begin(),v.end());
    21 
    22 
    23     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    24     {
    25         cout << *it << " ";
    26     }
    27     cout << endl;
    28     return 0;
    29 }

      排序(<algorithm> sort)

      sort(v.begin(),v.end())

    View Code
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 
     5 using namespace std;
     6 
     7 bool Comp(const int &a,const int &b)
     8 {
     9     return a>b;
    10 }
    11 
    12 int main()
    13 {
    14     vector<int> v;
    15     v.push_back(1);
    16     v.push_back(3);
    17     v.push_back(2);
    18     v.push_back(55);
    19     v.push_back(-1);
    20     v.push_back(0);
    21     v.push_back(2);
    22     v.push_back(3);
    23     v.push_back(4);
    24 
    25     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    26     {
    27         cout << *it << " ";
    28     }
    29     cout << endl;
    30 
    31     //默认升序
    32     sort(v.begin(),v.end());
    33 
    34 
    35     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    36     {
    37         cout << *it << " ";
    38     }
    39     cout << endl;
    40 
    41     //用降序 需要自定义一个降序函数
    42     sort(v.begin(),v.end(),Comp);
    43 
    44 
    45     for(vector<int>::iterator it = v.begin(); it != v.end(); ++it)
    46     {
    47         cout << *it << " ";
    48     }
    49     cout << endl;
    50 
    51     return 0;
    52 }

      字符串(<string>)

      输入

    View Code
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     string s1;
    10     s1 = "hello";
    11 
    12     string s2;
    13     char s[1024];
    14     //scanf 输入速度比cin快的多
    15     //scanf 是C函数,不可以输入string
    16     scanf("%s",s);
    17     s2 = s;
    18 
    19     cout << s1 << endl;
    20     cout << s2 << endl;
    21 
    22     return 0;
    23 }

     

      尾部添加字符字符串直接用+号 例如: s += 'a'; s += "abc",或者使用append方法,s.append(“123”)

      删除 (erase clear)

      s.erase(it + 1,it + 4); clear()

    View Code
     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     string s;
     9     s = "0123456789";
    10     cout << s << endl;
    11 
    12     string::iterator it = s.begin();
    13 
    14     //删除s[3]
    15     s.erase(it+3);
    16     cout << s << endl;
    17 
    18     //删除s[1]~s[3]
    19     s = "0123456789";
    20     s.erase(it + 1,it + 4);
    21     cout << s << endl;
    22 
    23     //全部删除
    24     s.clear();
    25     cout << "clear : " << s << endl;
    26 
    27     return 0;
    28 }

      查找(find)

      用find找到string里面第一个要找到元素(char或者串),找到返回数组下标,找不到返回end()迭代器

      string和vector有很多相同的东西,比如length(),size(),empty(),reverse(),相对也容易,就不一一说了。

      数字化处理(string)

      经常会遇到这样一种情况,有一个数字,需要把每一位给提取出来,如果用取余数的方法,花费的时间就会很长,所以可以当成字符串来处理,方便、省时。

      例子:求一个整数各位数的和

    View Code
     1 #include<iostream>
     2 #include<string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     string s;
     9     s = "123456789";
    10     int sum = 0;
    11     for(int i = 0; i < s.size(); ++i)
    12     {
    13         switch(s[i])
    14         {
    15             case '1': sum += 1;break;
    16             case '2': sum += 2;break;
    17             case '3': sum += 3;break;
    18             case '4': sum += 4;break;
    19             case '5': sum += 5;break;
    20             case '6': sum += 6;break;
    21             case '7': sum += 7;break;
    22             case '8': sum += 8;break;
    23             case '9': sum += 9;break;
    24         }
    25     }
    26     
    27     cout << sum << endl;
    28     
    29     return 0;
    30 }

      string与char *

      

    View Code
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     string s_string;
    10     char s_char[1000];
    11     scanf("%s",s_char);
    12 
    13     s_string = s_char;
    14 
    15     //printf输出char* 时用c_str处理
    16     printf(s_string.c_str());
    17     cout << endl;
    18 
    19     printf("%s",s_char);
    20     cout << endl;
    21 
    22     cout << s_char << endl;
    23     cout << s_string << endl;
    24     return 0;
    25 }

      sscanf

      

    View Code
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     string s1,s2,s3;
    10     char sa[100],sb[100],sc[100];
    11     sscanf("abc 123 wcd","%s%s%s",sa,sb,sc);
    12     s1 = sa;
    13     s2 = sb;
    14     s3 = sc;
    15     cout << s1 << " " << s2 << " " << s3 << endl;
    16 
    17     //将字符串分离成数字,分隔符为',''$'
    18     int a,b,c;
    19     sscanf("4,5$6","%d,%d$%d",&a,&b,&c);
    20     cout << a << " " << b << " " << c << endl;
    21     return 0;
    22 }

      string与数值相互转换( sprintf <sstream> )

    View Code
     1 #include<iostream>
     2 #include<string>
     3 #include<sstream>
     4 #include<cstdio>
     5 
     6 using namespace std;
     7 
     8 //c++ 方法 把数转换为string
     9 string converToString(double x)
    10 {
    11     ostringstream o;
    12     if( o << x)
    13     {
    14         // str()没有'' c_str有
    15         return o.str();
    16     }
    17     return "error";
    18 }
    19 
    20 double converFromString(const string &s)
    21 {
    22     istringstream i(s);
    23     double x;
    24     if( i >> x)
    25     {
    26         return x;
    27     }
    28     //if error
    29     return 0.0;
    30 }
    31 int main()
    32 {
    33     char b[100];
    34     string s1;
    35 
    36     //c语言方法
    37     sprintf(b,"%d",1987);
    38     s1 = b;
    39     cout << s1 << endl;
    40 
    41     string s2 = converToString(1954);
    42     cout << s2 << endl;
    43 
    44     string s3 = "202";
    45     int c = converFromString(s3);
    46     cout << c << endl;
    47 
    48     string s4 = "casacsa6";
    49     int d = converFromString(s4);
    50     cout << d << endl;
    51 
    52     string s5 = "21abf4";
    53     int f = converFromString(s5);
    54     cout << f << endl;
    55 
    56     return 0;
    57 }

      set容器

      set是用红黑树的平衡二叉索引树的数据结构来实现的,插入时,它会自动调节二叉树排列,把元素放到适合的位置,确保每个子树根节点的键值大于左子树所有的值、小于右子树所有的值,插入重复数据时会忽略。set迭代器采用中序遍历,检索效率高于vector、deque、list,并且会将元素按照升序的序列遍历。set容器中的数值,一经更改,set会根据新值旋转二叉树,以保证平衡,构建set就是为了快速检索(python中的set一旦建立就是一个常量,不能改的)。

      multiset,与set不同之处就是它允许有重复的键值。

      正反遍历,迭代器iterator、reverse_iterator

    View Code
     1 #include<iostream>
     2 #include<set>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     set<int> v;
     9     v.insert(1);
    10     v.insert(3);
    11     v.insert(5);
    12     v.insert(2);
    13     v.insert(4);
    14     v.insert(3);
    15 
    16     //中序遍历 升序遍历
    17     for(set<int>::iterator it = v.begin(); it != v.end(); ++it)
    18     {
    19         cout << *it << " ";
    20     }
    21     cout << endl;
    22 
    23     for(set<int>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
    24     {
    25         cout << *rit << " ";
    26     }
    27     cout << endl;
    28 
    29     return 0;
    30 }

      自定义比较函数,insert的时候,set会使用默认的比较函数(升序),很多情况下需要自己编写比较函数。

      1、如果元素不是结构体,可以编写比较函数,下面这个例子是用降序排列的(和上例插入数据相同):

    View Code
     1 #include<iostream>
     2 #include<set>
     3 
     4 using namespace std;
     5 
     6 struct Comp
     7 {
     8     //重载()
     9     bool operator()(const int &a, const int &b)
    10     {
    11         return a > b;
    12     }
    13 };
    14 int main()
    15 {
    16     set<int,Comp> v;
    17     v.insert(1);
    18     v.insert(3);
    19     v.insert(5);
    20     v.insert(2);
    21     v.insert(4);
    22     v.insert(3);
    23   
    24     for(set<int,Comp>::iterator it = v.begin(); it != v.end(); ++it)
    25     {
    26         cout << *it << " ";
    27     }
    28     cout << endl;
    29 
    30     for(set<int,Comp>::reverse_iterator rit = v.rbegin(); rit != v.rend(); ++rit)
    31     {
    32         cout << *rit << " ";
    33     }
    34     cout << endl;
    35 
    36     return 0;
    37 }

      2、元素本身就是结构体,直接把比较函数写在结构体内部,下面的例子依然降序:

    View Code
     1 #include<iostream>
     2 #include<set>
     3 #include<string>
     4 
     5 using namespace std;
     6 
     7 struct Info
     8 {
     9     string name;
    10     double score;
    11 
    12     //重载 <
    13     bool operator < (const Info &a) const
    14     {
    15         return a.score < score;
    16     }
    17 };
    18 int main()
    19 {
    20     set<Info> s;
    21     Info info;
    22 
    23     info.name = "abc";
    24     info.score = 123.3;
    25     s.insert(info);
    26 
    27     info.name = "EDF";
    28     info.score = -23.53;
    29     s.insert(info);
    30 
    31 
    32     info.name = "xyz";
    33     info.score = 73.3;
    34     s.insert(info);
    35     
    36     for(set<Info>::iterator it = s.begin(); it != s.end(); ++it)
    37     {
    38         cout << (*it).name << ":" << (*it).score << endl;
    39     }
    40     cout << endl;
    41 
    42     for(set<Info>::reverse_iterator rit = s.rbegin(); rit != s.rend(); ++rit)
    43     {
    44         cout << (*rit).name << ":" << (*rit).score << endl;
    45     }
    46     cout << endl;
    47 
    48     return 0;
    49 }

      multiset与set的不同之处就是key可以重复,以及erase(key)的时候会删除multiset里面所有的key并且返回删除的个数。

      map

      map也是使用红黑树,他是一个键值对(key:value映射),便利时依然默认按照key程序的方式遍历,同set。

    View Code
     1 #include<iostream>
     2 #include<map>
     3 #include<string>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     map<string,double> m;
    10 
    11     //声明即插入
    12     m["li"] = 123.4;
    13     m["wang"] = 23.1;
    14     m["zhang"] = -21.9;
    15     m["abc"] = 12.1;
    16     for(map<string,double>::iterator it = m.begin(); it != m.end(); ++it)
    17     {
    18         //first --> key second --> value
    19         cout << (*it).first << ":" << (*it).second << endl;
    20     }
    21     cout << endl;
    22     return 0;
    23 }

      用map实现数字分离

      string --> number

      之前用string进行过数字分离,现在使用map

    View Code
     1 #include<iostream>
     2 #include<map>
     3 #include<string>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     map<char,int> m;
    10 
    11     m['0'] = 0;
    12     m['1'] = 1;
    13     m['2'] = 2;
    14     m['3'] = 3;
    15     m['4'] = 4;
    16     m['5'] = 5;
    17     m['6'] = 6;
    18     m['7'] = 7;
    19     m['8'] = 8;
    20     m['9'] = 9;
    21     /*
    22         等价于
    23         for(int i = 0; i < 10; ++i)
    24         {
    25             m['0' + i] = i;
    26         }
    27     */
    28 
    29     string sa;
    30     sa = "9876543210";
    31     int sum = 0;
    32     for( int i = 0; i < sa.length(); ++i)
    33     {
    34         sum += m[sa[i]];
    35     }
    36     cout << sum << endl;
    37     return 0;
    38 }

      number --> string

    View Code
     1 #include <iostream>
     2 #include <map>
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     map<int,char> m;
    10 
    11     for(int i = 0; i < 10; ++i)
    12     {
    13         m[i] = '0' + i;
    14     }
    15 
    16     int n = 7;
    17 
    18     string out = "the number is :";
    19     cout << out + m[n] << endl;
    20 
    21     return 0;
    22 }

      multimap

      multimap由于允许有重复的元素,所以元素插入、删除、查找都与map不同。

      插入insert(pair<a,b>(value1,value2))

    View Code
     1 #include <iostream>
     2 #include <map>
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     multimap<string,double> m;
    10 
    11     m.insert(pair<string,double>("Abc",123.2));
    12     m.insert(pair<string,double>("Abc",123.2));
    13     m.insert(pair<string,double>("xyz",-43.2));
    14     m.insert(pair<string,double>("dew",43.2));
    15 
    16     for(multimap<string,double>::iterator it = m.begin(); it != m.end(); ++it )
    17     {
    18         cout << (*it).first << ":" << (*it).second << endl;
    19     }
    20     cout << endl;
    21 
    22     return 0;
    23 }

      至于删除和查找,erase(key)会删除掉所有key的map,查找find(key)返回第一个key的迭代器

      deque

      deque和vector一样,采用线性表,与vector唯一不同的是,deque采用的分块的线性存储结构,每块大小一般为512字节,称为一个deque块,所有的deque块使用一个Map块进行管理,每个map数据项记录各个deque块的首地址,这样以来,deque块在头部和尾部都可已插入和删除元素,而不需要移动其它元素。使用push_back()方法在尾部插入元素,使用push_front()方法在首部插入元素,使用insert()方法在中间插入元素。一般来说,当考虑容器元素的内存分配策略和操作的性能时,deque相对vectore更有优势。(下面这个图,我感觉Map块就是一个list< map<deque名字,deque地址> >)

      插入删除

      遍历当然可以使用下标遍历,在这里使用迭代器。

    View Code
     1 #include <iostream>
     2 #include <deque>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     deque<int> d;
     9 
    10     //尾部插入
    11     d.push_back(1);
    12     d.push_back(3);
    13     d.push_back(2);
    14     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    15     {
    16         cout << (*it) << " ";
    17     }
    18     cout << endl << endl;
    19 
    20     //头部插入
    21     d.push_front(10);
    22     d.push_front(-23);
    23     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    24     {
    25         cout << (*it) << " ";
    26     }
    27     cout << endl << endl;
    28 
    29     d.insert(d.begin() + 2,9999);
    30     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    31     {
    32         cout << (*it) << " ";
    33     }
    34     cout << endl << endl;
    35 
    36     //反方向遍历
    37     for(deque<int>::reverse_iterator rit = d.rbegin(); rit != d.rend(); ++rit )
    38     {
    39         cout << (*rit) << " ";
    40     }
    41     cout << endl << endl;
    42 
    43     //删除元素pop pop_front从头部删除元素 pop_back从尾部删除元素 erase中间删除 clear全删
    44     d.clear();
    45     d.push_back(1);
    46     d.push_back(2);
    47     d.push_back(3);
    48     d.push_back(4);
    49     d.push_back(5);
    50     d.push_back(6);
    51     d.push_back(7);
    52     d.push_back(8);
    53     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    54     {
    55         cout << (*it) << " ";
    56     }
    57     cout << endl;
    58 
    59     d.pop_front();
    60     d.pop_front();
    61     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    62     {
    63         cout << (*it) << " ";
    64     }
    65     cout << endl;
    66 
    67     d.pop_back();
    68     d.pop_back();
    69     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    70     {
    71         cout << (*it) << " ";
    72     }
    73     cout << endl;
    74 
    75     d.erase(d.begin() + 1);
    76     for(deque<int>::iterator it = d.begin(); it != d.end(); ++it )
    77     {
    78         cout << (*it) << " ";
    79     }
    80     cout << endl;
    81     return 0;
    82 }

      list

      list<int> l

      插入:push_back尾部,push_front头部,insert方法前往迭代器位置处插入元素,链表自动扩张,迭代器只能使用++--操作,不能用+n -n,因为元素不是物理相连的。

      遍历:iterator和reverse_iterator正反遍历

      删除:pop_front删除链表首元素;pop_back()删除链表尾部元素;erase(迭代器)删除迭代器位置的元素,注意只能使用++--到达想删除的位置;remove(key) 删除链表中所有key的元素,clear()清空链表。

      查找:it = find(l.begin(),l.end(),key)

      排序:l.sort()

      删除连续重复元素:l.unique() 【2 8 1 1 1 5 1】 --> 【 2 8 1 5 1】

      合并 I.merge(b)

      1)merge()是将两个有序的链表合并成另一个有序的链表,如果有一个链表不是有序的那么在执行代码时会报错:说链表不是有序的。

    2)还有,两个链表中的内容排序顺序与合并时采用的排序顺序必须一致,如果不一致,也会报错,说链表不是有序的。如想要降序合并两个链表,那么合并前的两个链表也必须是按降序排列的。
    3)另外,当执行完merge()后,右边的链表将变为空

    list<int> v1, v2;
    v2.merge(v1); //按照默认排序方法合并,即按照升序
    v2.merge(v1,greater<int>()); //按照降序合并

      

      bitset

      从来没用过,上两幅图吧就:

      stack(后进先出)

      这个印象深刻,学数据结构的时候做表达式求值的就是用的栈。

    View Code
     1 #include <iostream>
     2 #include <stack>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7 
     8     stack<int> s;
     9     s.push(1);
    10     s.push(2);
    11     s.push(4);
    12     s.push(5);
    13 
    14     cout << s.size() << endl;
    15 
    16     while(s.empty() != true)
    17     {
    18         cout << s.top() << endl;
    19         s.pop();
    20     }
    21     return 0;
    22 }

      stack然我唯一费解之处在于,貌似它没有iterator,可以试试s.begin()编译器报错的。

      queue(先进先出)

      queue有入队push(插入)、出队pop(删除)、读取队首元素front、读取队尾元素back、empty,size这几种方法

      priority_queue(最大元素先出)

    View Code
     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7 
     8     priority_queue<int> pq;
     9 
    10     pq.push(1);
    11     pq.push(3);
    12     pq.push(2);
    13     pq.push(8);
    14     pq.push(9);
    15     pq.push(0);
    16 
    17     cout << "size: " << pq.size() << endl;
    18 
    19     while(pq.empty() != true)
    20     {
    21         cout << pq.top() << endl;
    22         pq.pop();
    23     }
    24     return 0;
    25 }

      重载操作符同set重载操作符。

     
     
  • 相关阅读:
    SQL Server 数据库定时自动备份
    SQL SERVER 2012设置自动备份数据库
    SyncNavigator 数据库同步软件怎么用?
    SyncNavigator下载|SyncNavigator数据库同步软件 v8.4.1
    关于数据同步的几种实现
    课堂练习-找水王:
    软件工程第十四周总结
    Alpha版(内部测试版)发布
    软件工程第十三周总结
    意见评论汇总
  • 原文地址:https://www.cnblogs.com/shenben/p/5515594.html
Copyright © 2011-2022 走看看