C++ forward_list 庫 - assign() 函式



描述

C++ 函式std::forward_list::assign() 透過替換舊值來為 forward_list 分配新值。新元素在以下範圍中構造firstlast.

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::assign() 函式的宣告。

C++11

template <class InputIterator>
void assign (InputIterator first, InputIterator last);

引數

  • first - 輸入迭代器,指向範圍的初始位置。

  • last - 輸入迭代器,指向範圍的結束位置。

返回值

無。

異常

如果由firstlast指定的範圍無效,則結果未定義。

時間複雜度

線性,即 O(n)

示例

以下示例顯示了 std::forward_list::assign() 函式的使用方法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4, 5};
   forward_list<int> fl2;

   fl2.assign(fl1.begin(), fl1.end());

   cout << "List contains following elements" << endl;

   for (auto it = fl2.begin(); it != fl2.end(); ++it)
      cout << *it << endl;

   return 0;
}

讓我們編譯並執行以上程式,這將產生以下結果:

List contains following elements
1
2
3
4
5
forward_list.htm
廣告

© . All rights reserved.