在<<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 }
容器對象有一個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 }
這里需要留意的是,如果容器對象為空,則必須要先進行判斷,否則操作未定義。如上代碼是不能運行的。
刪除元素的操作如下,這里的代碼分別刪除容器中的偶數和奇數。刪除后打印容器。
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 }
刪除特定元素操作如下:
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 }
賦值操作如下,將一個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 }
