当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


C++ List list()用法及代码示例


描述

C++ 范围构造函数std::list::list()构造一个包含与范围内一样多的元素的列表第一的最后的

声明

以下是 std::list::list() 构造函数形式 std::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

相关用法


注:本文由纯净天空筛选整理自 C++ List Library - list() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。