顺序容器

一、顺序容器的相关操作:

示例:

 1 #include <iostream>
 2 #include <vector>
 3 #include <list>
 4 #include <deque>
 5 
 6 using namespace std;
 7 int main()
 8 {
 9     vector<int> a;
10     list<int>   b;
11     deque<int>  c;
12 
13     vector<int>::size_type      a1;
14     vector<int>::iterator       a2;
15     vector<int>::const_iterator a3;
16     vector<int>::reverse_iterator a4;
17     vector<int>::const_reverse_iterator a5;
18     vector<int>::difference_type a6;
19     vector<int>::value_type      a7;
20     vector<int>::reference       a8;
21     vector<int>::const_reference a9;
22 
23     list<int>::size_type        b1;
24     list<int>::iterator         b2;
25     list<int>::const_iterator   b3;
26     list<int>::reverse_iterator b4;
27     list<int>::reverse_iterator b4;
28     list<int>::const_reverse_iterator b5;
29     list<int>::difference_type b6;
30     list<int>::value_type      b7;
31     list<int>::reference       b8;
32     list<int>::const_reference b9;
33 
34     deque<int>::size_type       c1;
35     deque<int>::iterator            c2;
36     deque<int>::const_iterator   c3;
37     deque<int>::reverse_iterator c4;
38     deque<int>::const_reverse_iterator c5;
39     deque<int>::difference_type c6;
40     deque<int>::value_type      c7;
41     deque<int>::reference       c8;
42     deque<int>::const_reference c9;
43 
44     a.push_back(10);
45     a.push_back(20);
46     a.push_back(30);
47     a.push_back(40);
48     for(vector<int>::size_type i=0; i<a.size();++i){
49         cout << a[i] << endl;
50     }
51     c.push_back(10);
52     c.push_back(20);
53     c.push_back(30);
54     for(deque<int>::size_type ii=0; ii<c.size();++i){  //size_type,而不是用int类型
55         cout << c[ii] << endl;
56     }
57 
58     for(deque<int>::reverse_iterator iii=c.rbegin();  //反向迭代器
59         iii != c.rend();iii--)
60     {
61     }
62     return 0;
63 }

 二、顺序容器的拆入操作:

  1、容器元素中拆入的都是副本,不会影响原数据;

  2、插入操作可能会使得迭代器失效;

  3、避免存储end操作返回的迭代器;

迭代器失效示例代码:
1
vector<int> ivec; 2 ivec.push_back(10): 3 ivec.push_back(20): 4 ivec.push_back(30): 5 ivec.push_back(40): 6 7 vector<int>::iterator first = ivec.begin()++; 8 vector<int>::iterator last= ivec.end(); 9 10 while(first != last){    //error, ok:while(first != ivec.end(){
11 first = ivec.insert(first, 42); 12 ++first; 13 }

顺序容器拆入操作示例代码:

#include <iostream>
#include <vector>
#include <list>     //链表
#include <deque>    //双端数组
#include <string>

using namespace std;

int main()
{
    vector<string> svec;
    list<string>   slist;
    deque<string>  sdeque;

    //末端拆入
    svec.push_back("Bill");
    svec.push_back("Tom");
    svec.push_back("Mary");

    slist.push_back("Bill");
    slist.push_back("Tom");
    slist.push_back("Mary");

    sdeque.push_back("Bill");
    sdeque.push_back("Tom");
    sdeque.push_back("Mary");

    //前端拆入,向量vector没有push_front
    slist.push_front("111");
    slist.push_front("222");
    slist.push_front("333");

    sdeque.push_front("111");
    sdeque.push_front("222");
    sdeque.push_front("333");

    for(std::vector<string>::iterator it=svec.begin(); it!=svec.end(); ++it){
        cout << *it <<" ";
    }
    cout << std::endl;

    std::vector<string>::iterator it=svec.begin();
    it++;
    svec.insert(it, "Hello");
    svec.insert(it, 10, "Hello");
    for(std::vector<string>::iterator it=svec.begin(); it!=svec.end(); ++it){
        cout << *it <<" ";
    }
    cout << std::endl;

    string arr_str[] = {"hello", "8999", "888", "3444"};
    it=svec.begin();
    it++;
    svec.insert(it, arr_str+1, arr_str+3);
    for(std::vector<string>::iterator it=svec.begin(); it!=svec.end(); ++it){
        cout << *it <<" ";
    }
    cout << std::endl;、
  
    return 0;
}        

二、 顺序容器的关系运算符:

   1、比较的容器必须具有相同的容器类型;

   2、容器的比较是基于容器内元素的比较;

   3、容器内元素必须有相应的关系运算符;(对象的话,必须在类中实现>, < 等符号的重载);

 2 #include <iostream>
 3 #include <vector>
 4 
 5 class Dog 
 6 {
 7     //成员省略
 8     //必须>, >=, <, <=, ==, !=
 9 };
10 
11 int main()
12 {
13     vector<int> ivec1;
14     vector<int> ivec2;
15     vector<int> ivec3;
16     vector<int> ivec4;
17     vector<int> ivec5;
18     
19     ivec1.push_back(1);
20     ivec1.push_back(3);
21     ivec1.push_back(5);
22     ivec1.push_back(7);
23     ivec1.push_back(9);
24     ivec1.push_back(12);
25 
26     ivec1.push_back(0);
27     ivec1.push_back(2);
28     ivec1.push_back(4);
29     ivec1.push_back(6);
30     ivec1.push_back(8);
31     ivec1.push_back(11);
32 
33     /*  同类型之间进行比较
34      *  两个向量比较大小,如果向量中每个数都相等(个数要相等),这个两个向量才相等,否则不等;则从第一个依次开始比较,遇到不等则区分大小;
35      */
36     if(ivec1 > ivec2)
37     {
38         cout << "ivec1 大!"<< endl;
39     }
40     ivec4.push_back(1);
41     ivec4.push_back(3);
42     ivec4.push_back(5);
43     ivec1.push_back(7);
44 
45     vector<Dog> dogA;
46     vector<Dog> dogB;       //对象中必须重载关系运算符
47     dogA.push()
48 
49     return 0;
50 }

四、容器大小的操作:

   1、c.size();

   2、c.max_size();

   3、c.empty();

   4、c.resize(n);

   5、c.resize(n, t);

  注意:resize操作可能会使迭代器失效

 2 #include <iostream> 
 3 #include <vector> 
 4 #include <list> 
 5 #include <deque> 
 6  
 7 using namespace std; 
 8 int main() 
 9 { 
10     list<int> ilist; 
11     ilist.push_back(10); 
12     ilist.push_back(20); 
13     ilist.push_back(30); 
14      
15     cout <<"容器里数据的个数:" << ilist.size();     
16              
17     list<int>::size_type count = ilist.size(); 
18     cout <<"容器里数据的个数:" << ilist.size();        
19         
20     cout <<"容器的max_size:" << ilist.max_size();   
21     if(ilist.empty()){ 
22         cout << "容器是空的" ; 
23     }else{ 
24         cout << "容器是空的" ; 
25     }    
26         
27     ilist.resize(10);       //增加了元素默认值为0; 
28     for(list<int>::iterator it=ilist.begin(); it!=ilist.end();++it){ 
29         cout << *it << std::endl; 
30     }   
31     ilist.resize(20, -1);   //新增的值为-1 
32     for(list<int>::iterator it=ilist.begin(); it!=ilist.end();++it){ 
33         cout << *it << std::endl; 
34     }  
35 
36     //ilist.resize(0);      //清空容器
37     ilist.resize(10);       //缩小容器,删除了元素;
38     for(list<int>::iterator it=ilist.begin(); it!=ilist.end();++it){
39         cout << *it << std::endl;
40     }
41     return 0;
42 }

五、访问容器中的元素

  c.back();

  c.front();

  c[n];    //仅适用于vector, deque

  c.at(n);   //仅适用于vector, deque

 1 #include <list>
 2 #include <vector>
 3 #include <deque>
 4 #include <stdexcept>      //捕获异常时候,必须包含的文件
 5 
 6 using namespace std;
 7 int main()
 8 {
 9     vector<int> ivec;
10     ivec.push_back(10);
11     ivec.push_back(20);
12     ivec.push_back(30);
13     
14     cout << ivec.front() << endl;
15     cout << ivec.back()  << endl;
16     vector<int>::reference a = ivec.front();
17     vector<int>::reference b = ivec.back(); //返回值为容器内元素的引用类型  
18     
19     cout << a << endl;
20     cout << b  << endl;
21 
22     cout << *ivec.begin() << endl;              //通过迭代器获取数据
23     b = *ivec.begin();                          //begin()返回值为迭代器,即指针 
24     cout << b  << endl;
25     
26     cout << ivec[0] <<endl;     //只能vecto    cout << ivec[1] <<endl;
27     cout << ivec[2] <<endl;
28     //cout << ivec[3] <<endl;           //内存泄漏,不会抛出异常
29 
30     cout << ivec.at(0) <<endl;  //同样只能用于vector、deque
31     cout << ivec.at(1) <<endl;
32     cout << ivec.at(2) <<endl;
33     //cout << ivec.at(3) <<endl;            //at()会抛出异常
34 
35     try{
36         cout <<ivec.at(3) <<endl;
37     }catch(out_of_range){
38         cout <<"ivec out of range"<< endl;
39     }
40 
41 
42     list<int> ilist;
43     ilist.push_back(200);
44     if(!ilist.empty()){
45             cout << ilist.front() << endl;  //先判断容器是否为 空
46             cout << ilist.back() << endl;
47     }
48 
49     //cout << ilist[0] <<endl;      //error,list链表不能使用下标
50     return 0;   
51 }
52  

六、顺序容器的删除操作

  删除元素:

    c.erase(p);

    c.erase(b, e);    //删除区间为左闭右开区间,即b删除,e不删除

    c.clear();

    c.pop_back();    //从后面取走元素

    c.pop_front();    //从前面取走元素,不适用于vector,list,deque容器适用;

七、顺序容器的操作

  赋值与交换

    c1 = c2    //将容器c2中的数据拷贝到c1容器中:

    c1.swap(c2)  //将容器c2中的数据相互交换c1的数据

    c.assign(b, e) //赋值 

    c.assign(n, t)   //赋值

  使用assign:类型兼容即可

  使用swap: 类型必须相同

 2 #include <iostream>
 3 #include <list>
 4 #include <deque>
 5 #include <vector>
 6 #include <string>
 7 
 8 using namespace std;
 9 int main()
10 {
11     vector<int> a;
12     vector<int> b;
13     vector<int> c;
14     vector<char *> svec;
15     list<string> slist;
16     
17     a.push_back(10);
18     a.push_back(20);
19     a.push_back(30);
20     a.push_back(40);
21     
22     b.push_back(100);
23     b.push_back(200);
24     b.push_back(300);
25     
26     c.push_back(1000);
27     c.push_back(2000);
28     c.push_back(3000);
29     c.push_back(4000);
30     c.push_back(5000);
31     c.push_back(6000);
32     
33 
34     a.swap(b);
35     for(vector<int>::iterator it = a.begin(); it!=a.end();it++){
36         cout << *it << endl;
37     }
38 
39     a = b;
40     for(vector<int>::iterator it = a.begin(); it!=a.end();it++){
41         cout << *it << endl;
42     }
43 
44     vector<int>::iterator f = c.begin();
45     vector<int>::iterator e = c.end();
46     f++;
47     f++;
48     e--;
49 
50     a.assign(f, e);
51     for(vector<int>::iterator it = a.begin(); it!=a.end();it++){
52         cout << *it << endl;
53     }
54 
55     svec.push_back("apple");
56     svec.push_back("big");
57     svec.push_back("cat");
58 
59     slist.push_back("c");
60     slist.push_back("c++");
61     slist.push_back("java");
62     slist.push_back("c#");
63 
64     slist.assign(svec.begin(), svec.end()); //将字符指针转成string
65     for(list<string>::iterator it1 = slist.begin(); it1!= slist.end();it1++){
66         cout << *it1<< endl;
67     }
68  slist.assign(10, "APP");    //将字符指针转成string
69     for(list<string>::iterator it1 = slist.begin(); it1!= slist.end();it1++){
70         cout << *it1<< endl;
71     }
72     return 0;
73 }

 

  

 

posted on 2015-05-31 23:30  阳台  阅读(178)  评论(0编辑  收藏  举报

导航