List介绍

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

assign()                    // 给list赋值
back() // 返回最后一个元素
begin()             // 返回指向第一个元素的迭代器
clear()             // 删除所有元素
empty()             // 如果list是空的则返回true
end()              // 返回末尾的迭代器
erase()            // 删除一个元素
front()             // 返回第一个元素
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中重复的元素
get_allocator()           // 返回list的配置器

实例一:

#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

实例二:

#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 :
The contents of list3 :
list1.push_back() and list1.push_back():
The contents of list1 :
list1.push_front() and list1.push_front():
The contents of list1 :
list1.insert(list1.begin()+,,):
The contents of list1 :
list1.front()=
list1.back()=
list1.pop_front() and list1.pop_back():
The contents of list1 :
list1.erase(++list1.begin()):
The contents of list1 :
list2.assign(,):
The contents of list2 :
list1.max_size():
list1.size():
list1.empty():
The contents of list1 :
The contents of list3 :
list1>list3:
list1<list3:
The contents of list1 :
The contents of list1 :
The contents of list3 :
Press any key to continue

STL --> list用法的更多相关文章

  1. STL vector用法介绍

    STL vector用法介绍 介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和f ...

  2. STL set 用法

      c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. ...

  3. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

  4. 记一些stl的用法(持续更新)

    有些stl不常用真的会忘qwq,不如在这里记下来,以后常来看看 C++中substr函数的用法 #include<string> #include<iostream> usin ...

  5. 日常笔记6C++标准模板库(STL)用法介绍实例

    一.vector常见用法详解 vector翻译为向量,但是这里翻译成变长数组的叫法更好理解. 如果typename是一个STL容器,定义的时候要记得在>>符号之间加上空格,因为在C++11 ...

  6. c++ STL map 用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  7. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  8. STL vector 用法介绍

    介绍 这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  9. STL --> set用法

    set用法 一.set和multiset基础 set和multiset会根据特定的排序准则,自动将元素进行排序.不同的是后者允许元素重复而前者不允许. 需要包含头文件: #include <se ...

随机推荐

  1. R+openNLP︱openNLP的六大可实现功能及其在R语言中的应用

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- openNLP是NLP中比较好的开源工具,R语 ...

  2. HI3531的DDR3配置流程

    DDR3 初始化配置流程 系统上电之后,必须先完成DDR3 SDRAM 的初始化操作,系统才能访问DDR3 SDRAM.在进行初始化之前需要注意以下几点: 对DDR3 SDRAM 进行上电操作时,需要 ...

  3. yii学习笔记--快速创建一个项目

    下载yii框架 下载地址:http://www.yiiframework.com/ 中文网站:http://www.yiichina.com/ 解压文件

  4. Windows下的Memcache安装:

    Windows下的Memcache安装:1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在终端(也即cmd命令界面)下输入 'c:\memcac ...

  5. 错误卸载软件导致Windows7系统中的软件无法播放视频

    1.错误描述 2.错误原因 在卸载软件时,不小心将Windows7中的服务给删除了 3.解决办法 重新安装操作系统

  6. Java中的三目运算符

    1.问题背景    以下代码运行的结果是:    A.hai    B.1987    C.1988    D.以上答案都不对 /** * 三目运算符 * A.hai * B.1987 * C.198 ...

  7. Django学习-18-中间件

    为了验证中间件功能和函数执行过程,手动注册3个中间件到setting.py文件中 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddlewa ...

  8. hihocoder Challenge 29 B.快速乘法

    这题的题解和我写的有一拼,异常简洁,爆炸. 这题思路dp 表示的是讨论到第位,并比原数的前n位多了 显然j只能取0,1,毕竟2进制嘛 之后转移就好了,注意下面两个重要状态 #include <c ...

  9. java创建自定义类的数组

    今天在学图论的最小生成树,开始一直在想是用邻接矩阵还是关联矩阵来表示图,但是发现这样都会有好多空间浪费.于是我就自定义一个边的类,里面包含了权值,关联的端点1,端点2,和图的表示字母.发现我想创建11 ...

  10. FFT && 复数重载

    复数重载 与 FFT 1.复数重载: 重载了复数的运算,即重载了复数的加减乘以及赋初值. struct Complex{ //复数的重载 double r,i; IL Complex(){r = 0; ...