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. CSS3之background-clip

    1.属性简介 background-clip:padding|border|content|text|!important 2.兼容性 (1)IE6.7.8不兼容 (2)火狐3.0以上兼容 (3)Ch ...

  2. zTree实现获取一级节点数据

    zTree实现获取一级节点数据 1.实现源码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</t ...

  3. Error 1313: RETURN is only allowed in a FUNCTION SQL Statement

    1.错误描述 14:07:26 Apply changes to rand_string Error 1313: RETURN is only allowed in a FUNCTION SQL St ...

  4. (二十五)svn的问题

    今天更新代码到svn中的时候出现了错误,准确的说是在操作更新之前的步骤出现了错误,因此对svn有了更近一步的理解.    check:下载svn中的代码到指定的储存路径中:    update:更新s ...

  5. 系统架构以及需要导入的jar包

    架构: Servlet +JSP +JavaBean +JDBC 需要导入的jar包: MYSQL: 数据库驱动 C3PO连接池: (这个需要配置文件) C3PO DBUtils:特点:轻量级首选,增 ...

  6. C#图解教程 第九章 语句

    语句 什么是语句控制流语句if语句if-else语句while循环do循环for循环 for语句中变量的作用域初始化和迭代表达式中的多表达式 switch语句 分支示例switch语句补充分支标签 跳 ...

  7. LinkedHashMap和HashMap

    1.HashMap: HashMap里面存入的键值对在取出的时候是随机的,是比较常用的Map.它根据key的HashCode值存储数据,根据key可以直接取出它的值(当然也有冲突的情况,不过遍历链表就 ...

  8. 【BZOJ1834】网络扩容(最大流,费用流)

    [BZOJ1834]网络扩容(最大流,费用流) 题面 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用.求: 1. 在不扩容的情况下 ...

  9. 【BZOJ1012】【JSOI2008】最大数(线段树)

    [JSOI2008]最大数 题目描述 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作. 语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 限制:L不超过当前 ...

  10. [SDOI2015]星际战争

    水题啦 网络流+二分 误差才10^-3,乱搞直接开longlong暴力每个都乘1000,输出时除一下就好了 # include <bits/stdc++.h> # define IL in ...