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



描述

C++ 范围构造函数 std::list::list() 用来构造一个列表,其元素数量与范围内的元素数量相同。起始结束.

声明

以下是来自 std::list 头文件的 std::list::list() 构造函数的声明。

C++98

list (InputIterator first, InputIterator last,
      const allocator_type& alloc = allocator_type());

C++11

list (InputIterator first, InputIterator last,
      const allocator_type& alloc = allocator_type());

参数

  • first − 输入迭代器指向起始位置。

  • last − 输入迭代器指向结束位置。

  • 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.begin(), l1.end());

   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.