Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

list::list

公众成员函数  <list>

C++98; default (1)
explicit list (const allocator_type& alloc = allocator_type());
fill (2)
explicit list (size_type n, const value_type& val = value_type(),
                const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
  list (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
copy (4)
list (const list& x);
C++11; default (1)
explicit list (const allocator_type& alloc = allocator_type());
fill (2)
explicit list (size_type n);
         list (size_type n, const value_type& val,
                const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
  list (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
copy (4)
list (const list& x);
list (const list& x, const allocator_type& alloc);
move (5)
list (list&& x);
list (list&& x, const allocator_type& alloc);
initializer list (6)
list (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

构建列表

构造一个列表容器对象,根据使用的构造函数版本初始化它的内容:

C++98:
(1)空容器构造函数(默认构造函数)
构造一个空容器,没有元素。
(2)默认填充构造函数
构造一个包含n个元素的容器。每个元素都是val的副本。
(3)范围构造函数
构造与范围(first,last)相同数量的元素的容器,每个元素都由该范围中相应的元素以相同的顺序构造.
(4)拷贝构造函数
使用x中每个元素的副本以相同的顺序构造容器。
容器保留了alloc的内部副本,在整个生命周期中用于分配存储空间。
复制构造函数创建一个容器,并保存和使用x的分配器的副本。
元素的存储是使用这个内部分配器分配的。

☲  参数


alloc
分配器对象。
容器保存并使用这个分配器的内部副本.
成员类型allocator_type是容器使用的内部分配器类型, 在 forward_list中定义为第二个模板形参(Alloc)的别名.
如果allocator_type是默认分配器(没有状态)的实例化,则忽略考虑.
n
容器初始大小(即构造时容器中元素的数量)。
成员类型size_type是一个无符号整型。
val
填充容器的值.
容器中的n个元素的每一个都将被初始化为这个值的副本。
成员类型value_type是容器中元素的类型,在list中定义为其第一个模板形参(T)的别名。
first, last
输入迭代器初始和最终位置的范围。
使用的范围是[first,last),它包括first和last之间的所有元素, 包括first所指向的元素,但不包括last所指向的元素。
函数模板实参InputIterator必须是一个输入迭代器类型,该类型指向可以构造value_type对象的类型的元素。
x
用来复制或获取的一个相同类型的 list 对象(具有相同的类模板参数T和Alloc),其内容被复制或获取。
il
一个initializer_list对象。
这些对象是由初始化列表声明符自动构造的。
成员类型value_type是容器中元素的类型,在 list 中定义为其第一个模板形参(T)的别名。

☣  示例



// constructing lists
#include <iostream>
#include <list>

int main ()
{
  // constructors used in the same order as described above:
  std::list<int> first;                                // empty list of ints
  std::list<int> second (4,100);                       // four ints with value 100
  std::list<int> third (second.begin(),second.end());  // iterating through second
  std::list<int> fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {16,2,77,29};
  std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are: ";
  for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
    std::cout << *it << ' ';

  std::cout << '\n';

  return 0;
}

输出:
The contents of fifth are: 16 2 77 29

✥ 复杂度



对于默认构造函数(1)和move构造函数(5)(除非alloc与x的分配器不同)是常量。 对于所有其他情况,结果容器大小是线性的。

☣ 迭代器的有效性



移动构造函数(5),如果元素被移动,则使所有与x相关的迭代器、指针和引用失效。

⇄ 数据竞争


访问所有复制的元素。移动构造函数(5)修改x。

☂ 异常安全性



强保证:在抛出异常的情况下没有影响。
如果构造器不支持元素结构的适当参数, 或者如果[first,last)指定的范围无效,它将导致未定义的行为。

🍄  另请参阅



list::operator= 内容赋值 (公众成员函数)
list::assign 分配容器的内容(公众成员函数)
list::resize 改变大小(公众成员函数)
list::clear 清除内容 (公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图