C++ 容器对象vector和list 的使用

在<<c++ primer>>第四版Exercise Section 9.3.4 的Exercise 9.20 是这样的一道题目:编写程序判断一个vector<int> 容器包含的元素是否与list<int> 容器完全相同。测试代码如下:

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <string>
 4 #include <list>
 5 #include <deque>
 6 #include <vector>
 7 
 8 
 9 using namespace std;
10 
11 int main()
12 {
13     vector<int> vect;
14     list<int> li;
15     int vect_copy[] = {1,2,3,2,1,2,6};
16     int li_copy[] = {2,4,6,54,7,0,2};
17     vect.insert(vect.begin(),vect_copy, vect_copy+7);
18     li.insert(li.begin(),li_copy,li_copy+7);
19 
20     if (vect.size() != li.size())
21     {
22         cout << "it is different." << endl;
23         return 0;
24     }
25 
26     for (vector<int>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
27     {
28         bool flag = false;
29         for (list<int>::iterator begin_li = li.begin(); begin_li != li.end(); ++begin_li)
30         {
31             if (*begin == *begin_li)
32             {
33                 flag = true;
34                 continue;
35             }
36         }
37         if (flag)
38         {
39 
40         }
41         else
42         {
43             cout << *begin << " in vect is not in the list " << endl;
44         }
45     }
46     system("PAUSE");
47     return 0;
48 }
View Code

 

 容器对象有一个Insert成员函数,是用于在容器中插入元素使用,第一个参数是插入的位置,是个迭代器,后面两个参数是需要插入的元素迭代器开始和结尾。因为数组名是一个指针,因此这里也直接传入了数组名。

另外,这里有两个循环,分别遍历vector和list.

 

获取元素操作如下:

 1 #include "stdafx.h"
 2 #include <vector>
 3 #include <iostream>
 4 
 5 
 6 using namespace std;
 7 
 8 
 9 int main()
10 {
11     vector<int> vect;
12     int arry[] = {1,2,3,21,34,90};
13     vector<int> vect2;
14     vect.insert(vect.begin(),arry,arry+6);
15     cout << vect[0] << endl;
16     cout << vect.front() << endl;
17     cout << *vect.begin() << endl;
18     cout << vect2[0] << endl;
19     system("PAUSE");
20     return 0;
21 }
View Code

这里需要留意的是,如果容器对象为空,则必须要先进行判断,否则操作未定义。如上代码是不能运行的。

 

 删除元素的操作如下,这里的代码分别删除容器中的偶数和奇数。删除后打印容器。

 1 #include "stdafx.h"
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <list>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     int ia[] = {0,1,1,2,3,5,8,13,21,55,89};
12     list<int> li;
13     vector<int> vect;
14     li.insert(li.begin(),ia,ia+11);
15     vect.insert(vect.begin(),ia,ia+11);
16 
17 
18     // to operate the list object.
19     for (auto begin = li.begin(); begin != li.end();)
20     {
21         if ((*begin)%2 == 0)
22         {
23             begin = li.erase(begin);
24             continue;
25         }
26         ++begin;
27     }
28 
29     cout << "To print the list with odd number:" << endl;
30 
31     // print the list.
32     for (auto begin = li.begin(); begin != li.end(); ++begin)
33     {
34         cout << *begin << endl;
35     }
36 
37 
38     // to operate vector
39     for (vector<int>::iterator begin = vect.begin(); begin != vect.end();)
40     {
41         if (*begin % 2)
42         {
43             begin = vect.erase(begin);
44             continue;
45         }
46         ++begin;
47     }
48 
49     cout << "To print the vector with with even number:" << endl;
50     for (vector<int>::iterator begin = vect.begin(); begin != vect.end();++begin)
51     {
52         cout << *begin << endl;
53     }
54 
55     system("PAUSE");
56     return 0;
57 }
View Code

 

 

删除特定元素操作如下:

 1 #include "stdafx.h"
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <list>
 6 #include <string>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     string arry[] = {"zhi","haha","heihei","hehe","huhu"};
13     list<string> li;
14     li.insert(li.begin(),arry,arry+sizeof(arry)/sizeof(*arry));
15     list<string>::iterator ite = find(li.begin(),li.end(),"haha");
16     li.erase(ite);
17 
18     for (list<string>::iterator begin = li.begin(); begin != li.end(); ++begin)
19     {
20         cout << *begin << endl;
21     }
22     system("PAUSE");
23     return 0;
24 }
View Code

 

 

赋值操作如下,将一个vector中的元素拷贝到一个List 容器中。

 1 #include "stdafx.h"
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <list>
 6 #include <string>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     list<char*> li;
13     vector<string> vect;
14     
15     char *listarry[] = {"hello","world","Li","Zhi"};
16     string stringarry[] = {"heihei","haha","hehe","huhu"};
17     li.insert(li.begin(), listarry, listarry+4);
18     vect.insert(vect.begin(), stringarry, stringarry+4);
19 
20     cout << "The vector is : " << endl;
21     for (vector<string>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
22     {
23         cout << *begin << endl;
24     }
25 
26     cout << "The list is : " << endl;
27     for (list<char*>::iterator begin = li.begin(); begin != li.end(); ++begin)
28     {
29         cout << *begin << endl;
30     }
31 
32     vect.assign(li.begin(),li.end());
33 
34     cout << "after update. The vector is : " << endl;
35     for (vector<string>::iterator begin = vect.begin(); begin != vect.end(); ++begin)
36     {
37         cout << *begin << endl;
38     }
39 
40     system("PAUSE");
41     return 0;
42 }
View Code

 

posted on 2017-03-07 21:55  ^~~^  阅读(5119)  评论(0编辑  收藏  举报

导航