STL---list(列表)

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

list的类模板声明为

      template<class T, class Allocator=allocator<T>>class list;

与vector相同,list中的元素可以使任意型别的T,必须具备可设置可复制两个属性,包括int、double、string以及结构体或其他自定义的类型。

与vector的不同有如下几点:

1、list不支持随机读取

2、list的插入与删除操作非常快,插入和删除不会影响指向其他元素的指针、引用、迭代器,不会造成失效

3、list不提供下标操作符[]和at()函数

4、list没有提供容量、空间重新分配等操作函数,每个元素都有自己的内存

5、list提供了特殊的成员函数,专门用于移动元素。和同名的算法相比,其速度更快

list成员函数

assign() 给list赋值 

front() 返回第一个元素 
back() 返回最后一个元素 

push_back() 在list的末尾添加一个元素 
push_front() 在list的头部添加一个元素 

pop_back() 删除最后一个元素 
pop_front() 删除第一个元素 

size() 返回list中的元素个数 

max_size() 返回list能容纳的最大元素数量 

resize() 改变list的大小 


begin() 返回指向第一个元素的迭代器 
clear() 删除所有元素 
empty() 如果list是空的则返回true 
end() 返回末尾的迭代器 
erase() 删除一个元素 
insert() 插入一个元素到list中 


get_allocator() 返回list的配置器 

merge() 合并两个list 
rbegin() 返回指向第一个元素的逆向迭代器 
remove() 从list删除元素 
remove_if() 按指定条件删除元素 
rend() 指向list末尾的逆向迭代器 
reverse() 把list的元素倒转 
sort() 给list排序 
splice() 合并两个list 
swap() 交换两个list 
unique() 删除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 (2);   
    listOne.push_front (1);   
      
    //从后面向listOne容器中添加数据   
    listOne.push_back (3);   
    listOne.push_back (4);   
      
    //从前向后显示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(),0);   
    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;   
}   

 参考:   C++ List的用法(整理)        http://blog.csdn.net/lskyne/article/details/10418823

原文地址:https://www.cnblogs.com/wujing-hubei/p/5671473.html