Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.

assign() 给list赋值 
back() 返回最后一个元素 
begin() 返回指向第一个元素的迭代器 
clear() 删除所有元素 
empty() 如果list是空的则返回true 
end() 返回末尾的迭代器 
erase() 删除一个元素 
front() 返回第一个元素 
get_allocator() 返回list的配置器 
insert() 插入一个元素到list中 
max_size() 返回list能容纳的最大元素数量 
merge() 合并两个list 
pop_back() 删除最后一个元素 
pop_front() 删除第一个元素 
push_back() 在list的末尾添加一个元素 
push_front() 在list的头部添加一个元素 
rbegin() 返回指向第一个元素的逆向迭代器 
remove() 从list删除元素 
remove_if() 按指定条件删除元素 
rend() 指向list末尾的逆向迭代器 
resize() 改变list的大小 
reverse() 把list的元素倒转 
size() 返回list中的元素个数 
sort() 给list排序 
splice() 合并两个list 
swap() 交换两个list 
unique() 删除list中重复的元素

代码例子1:

 #include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
using namespace std; //创建一个list容器的实例LISTINT
typedef list<int> LISTINT;
//创建一个list容器的实例LISTCHAR
typedef list<int> LISTCHAR; void main()
{
//用list容器处理整型数据
//用LISTINT创建一个名为listOne的list对象
LISTINT listOne;
//声明i为迭代器
LISTINT::iterator i; //从前面向listOne容器中添加数据
listOne.push_front ();
listOne.push_front (); //从后面向listOne容器中添加数据
listOne.push_back ();
listOne.push_back (); //从前向后显示listOne中的数据
cout<<"listOne.begin()--- listOne.end():"<<endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl; //从后向后显示listOne中的数据
LISTINT::reverse_iterator ir;
cout<<"listOne.rbegin()---listOne.rend():"<<endl;
for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
cout << *ir << " ";
}
cout << endl; //使用STL的accumulate(累加)算法
int result = accumulate(listOne.begin(), listOne.end(),);
cout<<"Sum="<<result<<endl;
cout<<"------------------"<<endl; //--------------------------
//用list容器处理字符型数据
//-------------------------- //用LISTCHAR创建一个名为listOne的list对象
LISTCHAR listTwo;
//声明i为迭代器
LISTCHAR::iterator j; //从前面向listTwo容器中添加数据
listTwo.push_front ('A');
listTwo.push_front ('B'); //从后面向listTwo容器中添加数据
listTwo.push_back ('x');
listTwo.push_back ('y'); //从前向后显示listTwo中的数据
cout<<"listTwo.begin()---listTwo.end():"<<endl;
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char(*j) << " ";
cout << endl; //使用STL的max_element算法求listTwo中的最大元素并显示
j=max_element(listTwo.begin(),listTwo.end());
cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
}

结果:

listOne.begin()--- listOne.end():

listOne.rbegin()---listOne.rend():

Sum=
------------------
listTwo.begin()---listTwo.end():
B A x y
The maximum element in listTwo is: y
Press any key to continue

代码例子2:

#include <iostream>
#include <list> using namespace std;
typedef list<int> INTLIST; //从前向后显示list队列的全部元素
void put_list(INTLIST list, char *name)
{
INTLIST::iterator plist; cout << "The contents of " << name << " : ";
for(plist = list.begin(); plist != list.end(); plist++)
cout << *plist << " ";
cout<<endl;
} //测试list容器的功能
void main(void)
{
//list1对象初始为空
INTLIST list1;
//list2对象最初有10个值为6的元素
INTLIST list2(,);
//list3对象最初有3个值为6的元素
INTLIST list3(list2.begin(),--list2.end()); //声明一个名为i的双向迭代器
INTLIST::iterator i; //从前向后显示各list对象的元素
put_list(list1,"list1");
put_list(list2,"list2");
put_list(list3,"list3"); //从list1序列后面添加两个元素
list1.push_back();
list1.push_back();
cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
put_list(list1,"list1"); //从list1序列前面添加两个元素
list1.push_front();
list1.push_front();
cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
put_list(list1,"list1"); //在list1序列中间插入数据
list1.insert(++list1.begin(),,);
cout<<"list1.insert(list1.begin()+1,3,9):"<<endl;
put_list(list1,"list1"); //测试引用类函数
cout<<"list1.front()="<<list1.front()<<endl;
cout<<"list1.back()="<<list1.back()<<endl; //从list1序列的前后各移去一个元素
list1.pop_front();
list1.pop_back();
cout<<"list1.pop_front() and list1.pop_back():"<<endl;
put_list(list1,"list1"); //清除list1中的第2个元素
list1.erase(++list1.begin());
cout<<"list1.erase(++list1.begin()):"<<endl;
put_list(list1,"list1"); //对list2赋值并显示
list2.assign(,);
cout<<"list2.assign(8,1):"<<endl;
put_list(list2,"list2"); //显示序列的状态信息
cout<<"list1.max_size(): "<<list1.max_size()<<endl;
cout<<"list1.size(): "<<list1.size()<<endl;
cout<<"list1.empty(): "<<list1.empty()<<endl; //list序列容器的运算
put_list(list1,"list1");
put_list(list3,"list3");
cout<<"list1>list3: "<<(list1>list3)<<endl;
cout<<"list1<list3: "<<(list1<list3)<<endl; //对list1容器排序
list1.sort();
put_list(list1,"list1"); //结合处理
list1.splice(++list1.begin(), list3);
put_list(list1,"list1");
put_list(list3,"list3");
}

结果:

The contents of list1 :
The contents of list2 : 6 6 6 6 6 6 6 6 6 6
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1.push_back(2) and list1.push_back(4):
The contents of list1 : 2 4
list1.push_front(5) and list1.push_front(7):
The contents of list1 : 7 5 2 4
list1.insert(list1.begin()+1,3,9):
The contents of list1 : 7 9 9 9 5 2 4
list1.front()=7
list1.back()=4
list1.pop_front() and list1.pop_back():
The contents of list1 : 9 9 9 5 2
list1.erase(++list1.begin()):
The contents of list1 : 9 9 5 2
list2.assign(8,1):
The contents of list2 : 1 1 1 1 1 1 1 1
list1.max_size(): 1073741823
list1.size(): 4
list1.empty(): 0
The contents of list1 : 9 9 5 2
The contents of list3 : 6 6 6 6 6 6 6 6 6
list1>list3: 1
list1<list3: 0
The contents of list1 : 2 5 9 9
The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9
The contents of list3 :
Press any key to continue

C++中List的用法的更多相关文章

  1. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  2. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  3. SQL中distinct的用法

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  4. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  5. jQuery中Animate进阶用法(一)

    jQuery中animate的用法你了解多少呢?如果仅仅是简单的移动位置,显示隐藏,哦!天哪你在浪费资源!因为animate太强大了,你可以有很多意想不到的用法!让我们一起研究一下吧~~ 首先要了解j ...

  6. [转载]js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  7. js中this的用法

    经过近几周的模拟面试题,我查询了一些资料,今天就来说说,在js中this的用法吧.方法有四:第一,用作全局变量,第二,用作表该对象,第三,用作构造函数,第四,用作call和applay

  8. jQuery中eq()方法用法实例

    本文实例讲述了jQuery中eq()方法用法.分享给大家供大家参考.具体分析如下: 此方法能够获取匹配元素集上的相应位置索引的元素. 匹配元素集上元素的位置索引是从0开始的. 语法结构: 复制代码 代 ...

  9. php中return的用法实例分析

    本文实例讲述了php中return的用法.分享给大家供大家参考.具体分析如下: 首先,它的意思就是返回;return()是语言结构而不是函数,仅在参数包含表达式时才需要用括号将其括起来.当返回一个变量 ...

  10. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

随机推荐

  1. mysql插入数据时,去掉重复的数据;

    1. 利用insert ignore into语句去重 mysql> INSERT IGNORE INTO person_tbl (last_name, first_name) -> VA ...

  2. 【原创】MySQL常用脚本整理

    #1.查看表容量空间 ) ) ) AS 'free_size(G)' FROM information_schema.tables WHERE TABLE_SCHEMA='test' AND TABL ...

  3. 【SQL】182. Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  4. map赋值前要先初始化:assignment to entry in nil map

    注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误: test := make(map[string]map[int]i ...

  5. python中%r和%s的区别

    %r用rper()方法处理对象 %s用str()方法处理对象 有些情况下,两者处理的结果是一样的,比如说处理int型对象. 例一: print "I am %d years old.&quo ...

  6. 概率论中常见分布总结以及python的scipy库使用:两点分布、二项分布、几何分布、泊松分布、均匀分布、指数分布、正态分布

    概率分布有两种类型:离散(discrete)概率分布和连续(continuous)概率分布. 离散概率分布也称为概率质量函数(probability mass function).离散概率分布的例子有 ...

  7. 【Vijos 1998】【SDOI 2016】平凡的骰子

    https://vijos.org/p/1998 三维计算几何. 需要混合积求四面体体积: 四面体剖分后合并带权重心求总重心: 四面体重心的横纵坐标是四个顶点的横纵坐标的平均数: 三维差积求平面的法向 ...

  8. POJ 3678 Katu Puzzle 2-SAT 强连通分量 tarjan

    http://poj.org/problem?id=3678 给m条连接两个点的边,每条边有一个权值0或1,有一个运算方式and.or或xor,要求和这条边相连的两个点经过边上的运算后的结果是边的权值 ...

  9. 优客365 v2.9版本 后台存在SQL注入

    安装 打开后台登陆界面 http://localhost:9096/yk365/system/login.php 输入单引号报错 得到表名 经过跟踪后在\module\login.php文件出现错误 ...

  10. Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力

    D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...