// assignment operator with lists
#include <iostream>
#include <list>
int main ()
{
std::list<int> first (3); // list of 3 zero-initialized ints
std::list<int> second (5); // list of 5 zero-initialized ints
second = first;
first = std::list<int>();
std::cout << "Size of first: " << int (first.size()) << '\n';
std::cout << "Size of second: " << int (second.size()) << '\n';
return 0;
}
两个包含int元素的list容器都被初始化为不同大小的序列。
然后,second被赋值给first,所以现在两者相等并且大小为3。
最后,first被赋值给一个新构造的空容器对象,所以它的大小最后是0。
输出:
Size of first: 0
Size of second: 3