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



描述

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

声明

以下是来自 std::list 头文件的 std::list::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
list.htm
广告

© . All rights reserved.