出自http://www.cnblogs.com/yuehui/archive/2012/06/15/2550449.html

List容器
双向线性表list容器
   list类定义了双向的线性表。Vector类支持随机访问,但list只能支持顺序访问,由于list是双向的,因此我们可以按从前到后或者从后到前的顺序来访问list。
1.创建一个list对象
#include<list>
using namespace std;
     
list<int> lst1;
list<char> lst2;
2.向list对象中添加数值
从前面添加:
         lst1.push_front(0);
         lst1.push_front(1);
从后面添加:
   lst1.push_back(0);
         lst1.push_back(1);
3.删除操作
删除list对象中的第一个元素
  lst1.pop_front();
删除list对象中的最后一个元素
  lst1.pop_back();
4.获得list对象的存储容量
 lst1.size();
5.获得list对象中第一个和最后一个元素
 list<int>::iterator p = lst1.begin();
        list<int>::iterator p = lst1.end();
6.在list对象中插入元素
        list<int>::iterator p = lst1.begin();
        p++;
        lst1.insert(p, 2 , 100);
7.在list对象中删除元素;
 list<int>::iterator p1 = lst1.begin();
        list<int>::iterator p2 = lst1.begin();
        for (i=0;i<5;i++) p2++;
        lst1.erase(p1, p2);
8.访问list对象中的内容
 list<int>::iterator p = lst1.begin();
        while (p!=lst1.end())
 {
        *p = *p + 100;
         p++;
        }
9.将list对象中的内容排序;
 lst1.sort();

list应用问题

要求使用list解题
    输入:第一个行为总组数,从第二行开始为每组数,每组数的第一个数为该组数的个数。
    输出:将所有组的数排序无重复输出。
    输入
    2
    3
    3
    4
    5
    5
    1
    2
    3
    4
    5
    输出
    1 
    2
    3
    4
    5

程序源码:

#include<iostream>
#include<list>
using namespace std;

int main()
{
 //定义一个整型的list对象lst
 list<int> lst;
 int i;
 
 //往lst头和尾插入整数
 lst.push_front(10);
 lst.push_front(20);
 lst.push_front(30);
    lst.push_back(1);
 lst.push_back(2);
 lst.push_back(3);

for (i=0; i<=10;i++)  lst.push_back(i);

//输出lst中的所有元素
 //定义一个迭代器p
 list<int>::iterator p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;
 
 //将lst中的元素排序
 lst.sort();

p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;

//删除lst中的重复元素
 lst.unique();
 
 p = lst.begin();
 while (p!=lst.end())
 {
  cout << *p << "  ";
  p++;
 }
 cout << endl;

return 0;

}

最新文章

  1. Linux上Tomcat部署JavaWeb项目
  2. 面试复习(C++)之快速排序
  3. T4 assembly
  4. spring @qualifier注解
  5. ZJ2008树的统计(树链剖分)
  6. 【原】Redis-LRU缓存
  7. iOS-OC-基础-NSNumber常用方法
  8. 关于js作用域
  9. ServiceStack.Hello——跨平台.net REST api服务搭建
  10. hadoop pig入门总结
  11. Spring依赖注入的简化配置
  12. Jquery地图热点效果-鼠标经过弹出提示信息
  13. HADOOP集群配置
  14. ROS_Kinetic_28 turtlebot gazebo demo例子
  15. Quartz.net 3.x使用总结(二)——Db持久化和集群
  16. Anacond win10安装与介绍
  17. Markdown新手教程
  18. @synchronized深入理解
  19. appium学习记录1
  20. sparkStrming 实时插入 mysql 今天使用echart 实现了简单数据展示 很low 但学习必须加深

热门文章

  1. sql中取出字符串中数字
  2. 透彻理解并掌握JavaScript的this
  3. leetcood学习笔记-45-跳跃游戏二
  4. 校园商铺-2项目设计和框架搭建-9验证Service
  5. 阿里云宣布 Serverless 容器服务 弹性容器实例 ECI 正式商业化
  6. 1003CSP-S模拟测试赛后总结
  7. 使用OxyPlot在WPF中创建图表
  8. 二分查找总结及部分Lintcode题目分析 2
  9. VS2010-MFC(Ribbon界面开发:创建Ribbon样式的应用程序框架)
  10. elasticsearch的索引清理