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.