代码如下:

#include <iostream>
#include <string>
#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;
}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(L1.begin(), L1.end());printList(L2);list<int>L3(L2);printList(L3);list<int>L4(10, 100);printList(L4);}int main()
{test01();return 0;
}

测试结果:

代码如下:

#include <iostream>
#include <string>
#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;
}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;printList(L2);list<int>L3;L3.assign(L2.begin(), L2.end());printList(L3);list<int>L4;L4.assign(10, 100);printList(L4);}//交换
void test02()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;L2.assign(10, 100);cout << "交换前" << endl;printList(L1);printList(L2);cout << endl;L1.swap(L2);cout << "交换后" << endl;printList(L1);printList(L2);
}int main()
{test01();test02();return 0;
}

测试结果:

代码如下:

#include <iostream>
#include <string>
#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;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);if (L1.empty()){cout << "L1 empty" << endl;}else{cout << "L1 no empty" << endl;cout << "L1 capacity = " << L1.size() << endl;}L1.resize(10);printList(L1);L1.resize(2);printList(L1);}int main()
{test01();return 0;
}

测试结果:

总结:


代码如下:

#include <iostream>
#include <string>
#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;
}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);l.pop_back();printList(l);l.pop_front();printList(l);list<int>::iterator it = l.begin();l.insert(++it, 1000);printList(l);it = l.begin();l.erase(++it);printList(l);l.push_back(10000);l.push_back(10000);l.push_back(10000);printList(l);l.remove(10000);printList(l);l.clear();printList(l);}int main()
{test01();return 0;
}

测试结果:

总结:

代码如下:

#include <iostream>
#include <string>
#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;
}void test01()
{list<int>l1;l1.push_back(10);l1.push_back(20);l1.push_back(30);l1.push_back(40);//cout<<l1.at(0)<<endl;//错误,不支持at访问数据//cout<<l1[0]<<endl;//错误,不支持[]方式访问数据cout << "front elem = " << l1.front() << endl;cout << "back elem = " << l1.back() << endl;//list容器的迭代器是双向迭代器,不支持随机访问list<int>::iterator it = l1.begin();//it = it+1;//错误,不可以跳跃访问,即便是+1}int main()
{test01();return 0;
}

测试结果:

总结:


代码如下:

#include <iostream>
#include <string>
#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;
}bool cmp(int a, int b)
{return a > b;
}void test01()
{list<int>l;l.push_back(90);l.push_back(30);l.push_back(20);l.push_back(70);printList(l);l.reverse();printList(l);l.sort();printList(l);l.sort(cmp);printList(l);}int main()
{test01();return 0;
}

测试结果:

总结:

反转 — reverse
排序 — sort(成员函数)

[C++STL]list容器用法介绍相关推荐

  1. [C++STL]set容器用法介绍

    代码如下: #include <iostream> #include <set> using namespace std;void printSet(set<int> ...

  2. [C++STL]deque容器用法介绍

    代码如下: #include <iostream> #include <string> #include <deque> using namespace std;v ...

  3. [C++STL]vector容器用法介绍

    代码如下: #include <iostream> #include <string> #include <vector> using namespace std; ...

  4. [C++STL]map容器用法介绍

    代码如下: #include <iostream> #include <string> #include <map> using namespace std;voi ...

  5. [C++STL]queue容器用法介绍

  6. [C++STL]stack容器用法介绍

  7. [C++STL]string容器用法介绍

    string构造函数 代码如下: #include <iostream> #include <string> using namespace std;void test01() ...

  8. [C++ STL] 各容器简单介绍

    [C++ STL] 各容器简单介绍 目录 一.什么是STL? 二.容器(Containers) 2.1 vector 2.2 deque 2.3 list 2.4 set 2.5 map 2.6 容器 ...

  9. oracle 删除 queue,C++ stl队列Queue用法介绍:删除,插入等操作代码举例

    c++队列queue模板类的定义在头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型. C++队列Queue是一种容 ...

最新文章

  1. python opencv转换bytesio
  2. 【Android工具】安卓应用市场哪家强?chrome浏览器apk下载插件,play安装包下载,妈妈再也不用担心我找不到安装包了...
  3. HighNewTech:横向、纵向动图查看《Why资本寒冬》——根据中国四大行每年(2004年~2018年)贷款主要流向来看当下的资本寒冬
  4. 【转】Phong和Blinn-Phong光照模型
  5. 秒杀多线程第十二篇 多线程同步内功心法——PV操作上
  6. Python面试题大全(四):数据库篇
  7. C语言程序设计期末卷子,C语言程序设计试题1
  8. Openldap 整合windows AD认证
  9. Objective-C 方法重载 - Selector 标识起决定作用
  10. 粤嵌星计划打卡第三十二天(对象的销毁和垃圾收集机制)(java实现一个权限管理系统)
  11. 放弃有道词典和有道云笔记
  12. Linux常用命令分享
  13. CentOS安装配置freeIPA
  14. [学习笔记]opencv双线性插值法图像放大
  15. python爬虫实训总结报告_python爬虫简单总结(一)
  16. python学员管理系统
  17. JDBC SSL连接MySQL
  18. 网格交易法以及在数字货币中基于Python的量化实现
  19. tcl/tk参考——tcl内建命令
  20. 2023华为软件精英挑战赛,探寻软件人才与科技创新的最优解

热门文章

  1. 【ArcGIS微课1000例】0013:ArcGIS创建色带图例(以GlobeLand30土地覆盖数据为例)
  2. 【北斗】北斗卫星导航系统(BDS)介绍
  3. C#趣味程序---爱因斯坦的台阶问题
  4. C语言试题四十六之将m行n列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
  5. WireShark抓包之提示Alert Level: Fatal, Description: HandShake Failure
  6. 《零基础看得懂的C++入门教程 》——(1)第一个C++程序就让你知其所以然
  7. 计算机模拟考总结,高职单考单招计算机模拟一技术总结.doc
  8. php 点对点,浅析点对点(End-to-End)的场景文字识别
  9. python实例化是什么意思_Python中实例化class的执行顺序示例详解
  10. 三联《少年》创刊,各领域佼佼者畅言新知,帮少年建立思维素养体系!