C++ 列表庫 - operator= 函式



描述

C++ 函式 std::list::operator= 將一個列表的內容移動到另一個列表中。如有必要,它會修改列表的大小。

宣告

以下是來自 std::list 標頭檔案的 std::list::operator= 函式的宣告。

C++11

list& operator= (list&& other);

引數

other - 另一個相同型別的列表容器。

返回值

返回 this 指標。

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::list::operator= 函式的使用。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3, 4, 5};
   list<int> l2;

   l2 = move(l1);

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

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

   return 0;
}

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

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

© . All rights reserved.