清除向量并让一个向量与另一个向量相等

时间:2014-04-04 18:57:31

标签: c++ vector

对,我有两个向量。我想清空向量(列表)并用其他向量(templist)的内容替换它的内容,并在我完成时清除临时列表。这是正确的代码吗?

list.clear();
list = templist;
templist.clear();

2 个答案:

答案 0 :(得分:3)

这就是swap方法的用途:

list.swap(templist); // The contents are swapped
templist.clear(); // Clear the one you don't need anymore

请注意,这比list = templist快,因为不会复制内容。

答案 1 :(得分:0)

所有你需要的是:

list = templist;
templist.clear();
相关问题