List 容器
1.1 list基本概念
优点:可以对任意位置进行快速插入和删除元素
缺点:容器的遍历速度没有数组快。占用空间比数组大。
由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。
总结:STL中list和vector是两个最常被使用的容器,各有各的缺点。
1.2 list构造函数
#include <iostream> #include <list> using namespace std; void printList(const list<int> &L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } //list容器构造函数 void test01() { //创建list容器 list<int>L1; //添加数据 L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); //遍历容器: printList(L1); //区间方式构造: list<int>L2(L1.begin(), L1.end()); printList(L2); //拷贝构造 list<int>L3(L2); printList(L3); //n个elem list<int>L4(10, 1000); printList(L4); return; } int main() { test01(); system("pause"); return 0; }
1.3 list赋值和交换
#include <iostream> #include <list> using namespace std; //list容器赋值和交换 void printList(const list<int> &L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; return; } //list容器赋值和交换 void test01() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); printList(L1); list<int>L2; L2 = L1; //operator=赋值 printList(L2); list<int>L3; L3.assign(L2.begin(), L2.end()); printList(L3); list<int>L4; L4.assign(10, 100); printList(L4); return; } void test02() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); printList(L1); list<int>L2; L2.assign(10, 100); cout << "交换前:" << endl; printList(L1); printList(L2); L1.swap(L2); cout << "交换后:" << endl; printList(L1); printList(L2); return; } int main() { test01(); test02(); system("pause"); return 0; }
1.4 list大小操作
#include<iostream> #include<list> using namespace std; void printList(const list<int>&L){ for(list<int>::const_iterator it=L.begin();it!=L.end();it++){ cout<<*it<<" "; } cout<<endl; return; } //list 容器大小操作 void test01(){ list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); printList(L1); //判断容器是否为空 if(L1.empty()){ cout<<"L1不为空"<<endl; } else{ cout<<"L1不为空"<<endl; cout<<"L1的元素个数:"<<L1.size()<<endl; } //重新指定大小 L1.resize(10,10000); printList(L1);//10 20 30 40 10000 10000 10000 10000 10000 10000 L1.resize(2); printList(L1); return; } int main(){ test01(); system("pause"); return 0; }
1.5 list容器的插入和删除
#include <iostream> #include <list> using namespace std; void printList(const list<int> &L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; return; } //list容器的插入和删除 void test01() { list<int>L; //尾插 L.push_back(10); L.push_back(20); L.push_back(30); //头插 L.push_front(100); L.push_front(200); L.push_front(300); printList(L);//300 200 100 10 20 30 //尾删pop L.pop_back(); printList(L); //头删 L.pop_front(); printList(L); //insert插入 list<int>::iterator it = L.begin(); L.insert(++it, 1000); L.insert(L.begin(), 1000); printList(L);//200 1000 100 10 20 //删除 it = L.begin(); L.erase(++it); //200 100 10 20 printList(L); //移除 L.push_back(10000); L.push_back(10000); L.push_back(10000); L.push_back(10000); L.push_back(10000); printList(L); L.remove(10000);//将所有的10000全部删掉 printList(L); //清空 L.clear(); printList(L); return; } int main() { test01(); system("pause"); return 0; }
1.6 list数据存取
#include <iostream> #include <list> using namespace std; void test01() { list<int>L1; L1.push_back(10); L1.push_back(20); L1.push_back(30); L1.push_back(40); //L1[0]不可以用[]方式访问list容器中的元素 //L1.at(0)不可以用at方式访问list容器中的元素 //原因是list本质是链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的 cout << "第一个元素为:" << L1.front() << endl; cout << "最后一个元素为:" << L1.back() << endl; //验证迭代器是不支持随机访问的 list<int>::iterator it = L1.begin(); it++;//支持双向 //it=it+1//不支持随机访问 return; } int main() { test01(); system("pause"); return 0; }
总结: *list容器不可以通过[]或at方式访问数据 *返回第一个元素list--front *返回最有一个元素--back
1.7 list反转和排序
功能描述: 将容器中的元素反转,以及将容器中的数据进行排序。 函数原型: reverse();//反转链表 sort();//链表排序
#include <iostream> #include <list> #include <algorithm> using namespace std; void printList(const list<int> &L) { for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) { cout << *it << " "; } cout << endl; } //list容器反转和排序 void test01() { list<int>L1; L1.push_back(20); L1.push_back(10); L1.push_back(40); L1.push_back(50); L1.push_back(30); cout << "反转前:" << endl; printList(L1); //反转 cout << "反转后:" << endl; L1.reverse(); printList(L1); return; } bool myCompare(int v1, int v2) { //降序 就让第一个数>第二个数 return v1 > v2; } void test02() { list<int>L1; L1.push_back(20); L1.push_back(10); L1.push_back(40); L1.push_back(50); L1.push_back(30); cout << "排序前:" << endl; printList(L1); //排序 //所有不支持随机访问迭代器的容器,不可以用标准算法 //不支持随机访问迭代器的容器,内部会提供一些算法 //sort(L1.begin(), L1.end()); L1.sort();//默认规则,从小到大,升序 cout << "排序后:" << endl; printList(L1); L1.sort(myCompare); printList(L1); return; } int main() { test01(); test02(); system("pause"); return 0;