- 基本用法
#include<iostream> #include<time.h> #include<vector> #include<list> using namespace std; void main() { int a[] = {33,44,55,66,77,88}; int i; list<int> lst(a,a+5); list<int>::iterator it;//list只能用迭代器指针循环遍历 cout << endl; for (it=lst.begin();it!=lst.end();++it) { cout << *it<< " "; } cout << endl; list<int> lst2(5,0); lst2.assign(lst.begin(),lst.end()); for (it=lst2.begin();it!=lst2.end();it++) { cout << *it << " "; } list<int> lst3(6,8); int newnum[] = {10,12,24,36,46,54,99.88,111}; lst3.assign(newnum, newnum + 4); cout << endl; for (it=lst3.begin();it!=lst3.end();it++) { cout << *it << "*******"; } lst3.assign(&newnum[0], &newnum[3]); cout << endl; for (it = lst3.begin(); it != lst3.end(); it++) { cout << *it << "======"; } }
输出结果:
- 利用迭代器指针增/删元素
#include<iostream> #include<time.h> #include<vector> #include<list> using namespace std; void main() { list<int>lst3; int i; list<int>::iterator it; int newnum[] = {10,12,24,36,46,54,99.88,111}; for (i = 0; i < 5; i++) { lst3.push_back(newnum[i]); } //lst3.assign(&newnum[0], &newnum[3]); it = lst3.begin(); it++; lst3.insert(it,2,88); --it; lst3.insert(it, 666); cout << endl; for (it = lst3.begin(); it != lst3.end(); it++) { cout << *it << " "; } }
输出结果: