STL容器之list

list也是一个顺序容器。元素在容器线性排列。list中元素间的存储位置无关联,其元素的先后关系由链表指针表示。

list容器有如下特点:

  1. 在容器的任意位置高效的插入或删除元素(常量时间复杂度),而不仅仅是在头尾;
  2. 可以在容器内或容器间高效的移动元素(常量时间复杂度);
  3. 可以正向或反向遍历数组。

相较于其它两种顺序容器(vector和deque),list在插入删除移动元素时的效率较高,一般用于排序算法。但是list容器,不能直接访问某元素。比如我们要访问第6个元素,我们只能从第一个或最后一个元素,一个一个的遍历过去,不像其它两种顺序容器,可以使用下标直接访问。另外由于需要存储额外的指针,list的空间开销也较其它两种容器大,尤其是在存储大量小元素时。

此容器模版声明如下:

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

1、构造函数

此模板容器提供如下构造函数:

explicit list ( const Allocator& = Allocator() );
explicit list ( size_type n, const T& value = T(), const Allocator& = Allocator() );
template < class InputIterator >
         list ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
list ( const list<T,Allocator>& x );

2、迭代器函数

list容器提供begin()、end()、rbegin()、rend()四个迭起函数。基本用法同其它两种顺序容器。

3、容量函数

list容器提供以下容量函数:

empty,size,max_size,resize

4、元素访问

front

返回容器中的第一个元素。调用此函数,容器不能为空。

back

返回容器中的最后一个元素。调用此函数,容器不能为空。

5、修改函数


6、操作函数

代码示例:

// list::remove_if
#include <iostream>
#include <list>
using namespace std;

// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }

// a predicate implemented as a class:
class is_odd
{
public:
  bool operator() (const int& value) {return (value%2)==1; }
};

int main ()
{
  int myints[]= {15,36,7,17,20,39,4,1};
  list<int> mylist (myints,myints+8);   // 15 36 7 17 20 39 4 1

  mylist.remove_if (single_digit);      // 15 36 17 20 39

  mylist.remove_if (is_odd());          // 36 20

  cout << "mylist contains:";
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout << " " << *it;
  cout << endl;

  return 0;
}

带参数的版本,将提供一个比较器BianryPredicate,用法remove_if中的比较器类似。

代码示例;

// list::unique
#include <iostream>
#include <cmath>
#include <list>
using namespace std;

// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }

// a binary predicate implemented as a class:
class is_near
{
public:
  bool operator() (double first, double second)
  { return (fabs(first-second)<5.0); }
};

int main ()
{
  double mydoubles[]={ 12.15,  2.72, 73.0,  12.77,  3.14,
                       12.77, 73.35, 72.25, 15.3,  72.25 };
  list<double> mylist (mydoubles,mydoubles+10);
  
  mylist.sort();             //  2.72,  3.14, 12.15, 12.77, 12.77,
                             // 15.3,  72.25, 72.25, 73.0,  73.35

  mylist.unique();           //  2.72,  3.14, 12.15, 12.77
                             // 15.3,  72.25, 73.0,  73.35

  mylist.unique (same_integral_part);  //  2.72,  3.14, 12.15
                                       // 15.3,  72.25, 73.0

  mylist.unique (is_near());           //  2.72, 12.15, 72.25

  cout << "mylist contains:";
  for (list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout << " " << *it;
  cout << endl;

  return 0;
}


示例代码:
#include <list>
#include <iostream>

using namespace std;

int main ()
{
    int ar1[] = {1, 5, 3, 9, 7}; 
    int ar2[] = {2, 4, 8, 6, 0}; 

    list<int> a1 (ar1, ar1 + sizeof(ar1)/sizeof(int));
    list<int> a2 (ar2, ar2 + sizeof(ar2)/sizeof(int));

    a1.sort (); 
    a2.sort (); 

    a1.merge (a2);

    list<int> &first = a1; 
    cout << "first contains:";
    for (list<int>::iterator it=first.begin(); it!=first.end(); ++it)
        cout << " " << *it;
    cout << endl;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值