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. RS232 3线制与7线制的区别

    当通信距离较近时,可不需要Modem,通信双方可以直接连接,这种情况下,只需使用少数几根信号线.最简单的情况,在通信中根本不需要RS-232C的控制联络信号,只需三根线(发送线.接收线.信号地线)便可 ...

  2. SSH2三大框架SQL查询

    /** * 查询省份名称 * @author YHD * @return * @see */ @SuppressWarnings("unchecked") @Override pu ...

  3. Windows平台的PHP之开启COM配置

    Windows平台的PHP如果未配置COM,调用COM组件,错误如下 Fatal error: Class 'COM' not found in XXXXXXXXX php 根目录的 ext 文件夹下 ...

  4. Java之split方法

    Java之split方法 1.间隔号"." (1)str.split(".") String str = "10.156.35.87"; S ...

  5. Caused by: java.sql.SQLException: Operand should contain 1 column(s)

    1.错误描述 [ERROR:]2015-05-05 15:48:55,847 [异常拦截] org.hibernate.exception.DataException: error executing ...

  6. Codeforces Round #427 (Div. 2) D - Palindromic characteristics

    本题是个简单的区间dp 最近都没时间做题了,被我妈强制喊回去,然后颓废了10天(回家也没发控制住自己= = 我的锅),计划都打乱了,本来还报名了百度之星,然后没时间参加 #include<cma ...

  7. 搭建基于eclipse的纯的JavaWeb项目

    -----------2016.9.9--------------------- 步骤: 1.搭建一个Java项目 2,字该项目下新建一个文件夹,表示根,名字为webapp(name随意) 3,在we ...

  8. javascript学习记录-2-18

    对象定义的几种方法: var  person=new Object(); person.name="111"; person.age=22; 或 var person={   na ...

  9. 洛谷P3402 【模板】可持久化并查集(可持久化线段树,线段树)

    orz TPLY 巨佬,题解讲的挺好的. 这里重点梳理一下思路,做一个小小的补充吧. 写可持久化线段树,叶子节点维护每个位置的fa,利用每次只更新一个节点的特性,每次插入\(logN\)个节点,这一部 ...

  10. Emacs考场配置

    当年\(NOip\)考场配置不记得啦 存在这里搞事情 (set-background-color "gray15") (set-foreground-color "gra ...