一,基本概念
二,構造函數
//打印list容器元素函數
void printList(list<int> list1 ) {
for (list<int>::iterator it = list1.begin(); it != list1.end(); it++) {
cout << *it<<" ";
}
cout << endl;
cout << "************************************" << endl;
}
//list容器的構造的測試函數
void test01(){
list<int> t1(5, 9); //把5個9 放入list元素之中
list<int> t2(t1); //拷貝構造函數
list<int> t3;
t3.push_back(11);
t3.push_back(22);
t3.push_back(33);
t3.push_back(44);
printList(t1);
printList(t2);
printList(t3);
}
三,賦值和交換
void test02() {
list<int> t1;
t1.assign(5, 10); //將5個10賦值給t1
list<int> t2;
t2.assign(t1.begin(),t2.end()); //t2通過t1的迭代器進行賦值
list<int> t3;
t3.insert(t3.begin(),18);
cout << "交換前" << endl;
printList(t3);
printList(t1);
t3.swap(t1);
cout << "交換後" << endl;
printList(t3);
printList(t1);
}
四,大小操作
void test03() {
list<int>L1;
L1.push_back(10);
L1.push_back(90);
L1.push_back(30);
L1.push_back(40);
if (L1.empty()) {
cout << "列表爲空" << endl;
}
else {
cout << "列表不爲空" << endl;
cout << "列表的大小爲" << L1.size() << endl;
}
L1.resize(2); //重定義list的大小
cout << "修改容器後的元素個數" << L1.size() << endl;
L1.resize(5, 99); //重修修改大小,並以99zuo爲填充
cout << "修改容器後的元素個數" << L1.size() << endl;
printList(L1);
}
五,數據存取
六,反轉和排序
void test04() {
list<int>L1;
L1.push_back(10);
L1.push_back(90);
L1.push_back(30);
L1.push_back(40);
L1.sort(); //從小到大排序
printList(L1);
L1.sort(compare); //從大到小排序
printList(L1);
L1.reverse(); //反轉list
printList(L1);
}