C++ 列表库 - 运算符 = 函数


描述

C++ 函数std::list::operator=通过替换旧内容将新内容分配给列表。如果需要,它会修改列表的大小。

宣言

以下是 std::list::operator= 函数形式 std::list 标头的声明。

C++98

list& operator= (const list& other);

C++11

list& operator= (const list& other);

参数

other - 相同类型的另一个列表对象

返回值

返回此指针。

例外情况

该成员函数从不抛出异常。

时间复杂度

线性即 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 = 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
列表.htm