c++STL中的List容器详解
2020-03-05 17:30
204 查看
c++STL中的List容器详解
一、提要
List 是一个双向链表容器,List容器能够在任何地方快速的删除添加元素。
List 容器的接口与其他容器的接口相似,List容器不能随机数据存取元素(如:用下标或者用at.()方法都是不可以的),数据存取我有必要提一下,因为我以前也搞不懂,我今天问了老师才知道,数据存就是把数据存进去,修改它的值,取就是把数据取出来,这里存的什么值,使用迭代器时可以进行++操作,但不可以进行+3,+4操作,使用时需包含头文件 和deque 容器相同的操作时我只是复习一下的知识,就简单的提一下,不会去深讲,有需要的可以去看我之前发的deque容器详解
二、List 容器的构造函数
//List 容器默认构造函数和带参数的构造函数 void demo1(void) { //List默认构造函数 list<int> listInt; cout<<"listInt默认构造函数的元素个数:" << listInt.size() << endl; //List带参数的构造函数 list<int> listIntA(10,666); cout << "listIntA带参数的构造函数的元素个数:" << listIntA.size() << endl; vector<int> vectInt; vectInt.capacity(); //List 容器没有capacity() 容量函数 不存在预先分配空间 //List 使用迭代器访问元素 for (list<int>::iterator it = listIntA.begin(); it != listIntA.end(); it++) { cout <<"listIntA带参构造函数的元素:"<< (*it) << endl; } }
运行结果:
三、List 容器的头尾添加删除操作和数据的存取
//List 容器的头尾添加删除操作和数据的存取 void demo2(void) { list<int> listInt; //在尾部追加元素 listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //在头部删除元素 listInt.pop_front(); listInt.pop_front(); //在尾部删除元素 listInt.pop_back(); listInt.pop_back(); //头部添加元素 listInt.push_front(11); listInt.push_front(12); //使用迭代器遍历打印 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt添加删除过后:" << (*it) << endl; } cout << "------------数据存取------------" << endl; //数据的存取 listInt.back() = 14;//listInt的最后一个元素赋值为14 listInt.front() = 16;//listInt的前面第一个元素赋值为16 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt数据存取过后:" << (*it) << endl; } }
运行结果:
四、List 容器与迭代器
//List 容器与迭代器 void demo3(void) { list<int> listInt; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //使用普通的迭代器遍历打印 for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " -----------------常量迭代器---------------------" << endl; //使用常量迭代器遍历打印 for (list<int>::const_iterator cit = listInt.cbegin();cit != listInt.cend(); cit++) { cout << "listInt的元素:" << (*cit) << endl; } cout << " -----------------逆转迭代器---------------------" << endl; //使用逆转迭代器遍历打印 for (list<int>::reverse_iterator rit = listInt.rbegin(); rit != listInt.rend(); rit++) { cout << "listInt的元素:" << (*rit) << endl; } }
运行结果:
五、List 容器的赋值和大小
//List 容器的赋值和大小 void demo4(void) { list<int> listInt,listIntA,listIntB,listIntC; listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); //第一种 //指定区间,listInt.begin()到listInt.end()的元素,赋值给listIntB //注意:包括listInt.begin()但不包括listInt.end()指向的元素(左闭右开), listIntB.assign(listInt.begin(), listInt.end()); for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果:1,2,3,4,5 listIntB.end() 不是指向的5,是指向的5后面的元素 cout << "listIntB的元素:" << (*it) << endl; } //也可以用前置++ 前置-- listIntB.assign(++listInt.begin(), --listInt.end()); for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 2,3,4 cout << "listIntB使用前置--++赋值的元素:" << (*it) << endl; } //第二种, //指定元素个数和元素值 listIntC.assign(4,666); for (list<int>::iterator it = listIntC.begin(); it != listIntC.end(); it++) { //打印结果: 666,666,666,666, cout << "listIntC使用是指定元素个数和元素值赋值:" << (*it) << endl; } //第三种 使用赋值构造函数 listIntA = listIntC; for (list<int>::iterator it = listIntA.begin(); it != listIntA.end(); it++) { //打印结果: 666,666,666,666, cout << "listIntA使用赋值构造函数构造listIntC的元素:" << (*it) << endl; } cout << "---------------list容器的大小--------------" << endl; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. list<int> listIntD; listIntD.push_back(1); listIntD.push_back(2); listIntD.push_back(3); if (!listIntD.empty()) {//empty int size = listIntD.size();//size=3 listIntD.resize(2); //resize函数是重新定义元素个数//1,2 listIntD.resize(5);//1,2,0,0,0 listIntD.resize(7, 22);//1,2,0,0,0,22,22 } for (list<int>::iterator it = listIntD.begin(); it != listIntD.end(); it++) { //打印结果://1,2,0,0,0,22,22 cout << "listIntD的元素:" << (*it) << endl; } }
运行结果:
六、list 容器的插入和删除,还有反序排列
//list 容器的插入和删除,还有反序排列 void demo5(void) { list<int> listInt; list<int> listIntA(4, 333); listInt.push_back(1); listInt.push_back(2); listInt.push_back(3); listInt.push_back(4); listInt.push_back(5); cout << "-----------------------元素的插入----------------" << endl; //这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识. //list容器的插入 //第一个参数常常是需要插入的迭代器位置 cout << " 第一种 " << endl; //第一种: 指定区间插入 注意:左闭右开 listInt.insert(listInt.begin(), listIntA.begin(), listIntA.end()); for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " 第二种 " << endl; //第二种:指定元素个数和元素值 在2的位置插入2个666 listInt.insert(++listInt.begin(),2,666); for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << " 第三种 " << endl; //第三种:指定元素值 在最后的位置插入一个6 listInt.insert(listInt.end(), 6); for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) { cout << "listInt的元素:" << (*it) << endl; } cout << "--------------元素的删除------------------" << endl; list<int> listIntB; listIntB.push_back(1); listIntB.push_back(2); listIntB.push_back(3); listIntB.push_back(4); listIntB.push_back(5); listIntB.push_back(6); listIntB.push_back(7); cout << " 第一种 " << endl; //这里与deque容器有些不一样 //list容器 迭代器只能++或--不能执行+3,+4操作 list<int>::iterator begin = listIntB.begin(); list<int>::iterator end = listIntB.end(); --end; --end; ++begin; ++begin; //第一种: 指定区间删除 注意:左闭右开 //begin迭代器指向容器的第三个元素end指向容器的倒数第二个元素 //begin到end的元素删除 注意:左闭右开 listIntB.erase(begin,end);//erase()函数返回值是下一个位置的迭代器 for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 1,2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第二种" << endl; //第二种: 指定位置删除 //把容器的第一元素删除 listIntB.erase(listIntB.begin()); for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果: 2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第三种 " << endl; listIntB.push_back(4); listIntB.push_back(4); listIntB.push_back(4); listIntB.push_back(4); //第三种:删除指定元素 //会把元素值相同的全部删掉 listIntB.remove(4);//rmemove()方法 for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { //打印结果:还是 2 ,6,7, cout << "listIntB的元素:" << (*it) << endl; } cout << " 第四种 " << endl; //第四种:迭代器遍历删除 //迭代器遍历删除6这个元素 for (list<int>::iterator it = listIntB.begin(); it != listIntB.end();) { if (*it == 6) { //erase()函数返回值是下一个位置的迭代器,这时it已经指向了一个位置; it = listIntB.erase(it); } else { cout << "listIntB的元素:" << (*it) << endl; //erase()函数返回值是下一个位置的迭代器只有不经过上面的if() it++;//it++如果放在上面不行,越界了 } } cout << "--------------反序排列---------------" << endl; listIntB.push_back(1); listIntB.push_back(2); listIntB.push_back(3); listIntB.push_back(4); //reverse() 函数可以逆转链表如1,2,3,4,5,用reverse()函数之后就是5,4,3,2,1, listIntB.reverse(); for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) { cout << "listIntB的元素:" << (*it) << endl; } }
运行结果:
七、结尾
deque容器没有的还有忘写了的,全都在这代码的注释里了。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- STL之list容器详解
- C++ STL 提供了3个序列容器 :vector, deque, list
- C++学习 STL之二:vector容器用法详解
- [C++]STL-list容器
- STL 之 list 容器详解
- C++ STL基本容器的使用(vector、list、deque、map、stack、queue)
- c++几种常见STL容器比较和分析 hashmap, map, vector, list
- C++中STL容器之双向链表——list
- C++中STL(标准模板库)整理(容器:vector、deque、stack、queue、list、map、set)
- STL — list容器用法的详解
- c++的STL模板库中3种容器类:vector,list,deque的比较
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- C++学习 STL之四:list用法详解
- C++ 浅析 STL 中的 list 容器
- C++ STL 容器技术 之 list双向链表容器
- STL之list容器函数用法详解
- C++STL库list容器简单实现
- STL之list容器详解 http://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html
- C++ STL常见容器(vector list deque 顺序容器篇)
- 【C++ STL学习之四】容器list深入学习