C++ 列表库 - list() 函数


描述

C++ 复制构造函数std::list::list() 使用现有列表中存在的每个元素的副本构造一个列表。

宣言

以下是 std::list::list() 构造函数形式 std::list 标头的声明。

C++98

list (const list& other);

C++11

list (const list& other);
list (const list& other, const allocator_type& alloc);

参数

  • other - 相同类型的另一个列表对象。

  • alloc - 分配器对象。

返回值

构造函数永远不会返回值。

例外情况

该成员函数从不抛出异常。

时间复杂度

线性即 O(n)

例子

以下示例显示了 std::list::list() 构造函数的用法。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3, 4, 5};
   list<int> l2(l1);

   cout << "List contains following element" << endl;

   for (auto it = l2.begin(); it != l2.end(); ++it)
      cout << *it << endl;

   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

List contains following element
1
2
3
4
5
列表.htm