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