當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。