一,基本概念
二,构造函数
//打印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);
}