C++ Set 库 - operator= 函数
描述
它将新内容分配给容器,替换其当前内容。
声明
以下是 std::set::operator= 在各种 C++ 版本中的工作方式。
C++98
set& operator= (const set& x);
C++11
set& operator= (const set& x); set& operator= (set&& x); set& operator= (initializer_list<value_type> il)
返回值
它返回 *this。
异常
如果抛出异常,则容器处于有效状态。
时间复杂度
Linear in size of the container.
示例
以下示例显示了 std::set::operator= 的用法。
#include <iostream>
#include <set>
int main () {
int myints[] = { 10,20,30,40,50 };
std::set<int> first (myints,myints+10);
std::set<int> second;
second = first;
first = std::set<int>();
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
return 0;
}
上述程序将正确编译和执行。
Size of first: 0 Size of second: 8

