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
广告