gpt4 book ai didi

c++ - 清除 vector 并让一个 vector 等于另一个 vector

转载 作者:行者123 更新时间:2023-11-30 02:47:14 26 4
gpt4 key购买 nike

对了,我有两个 vector 。我想清空 vector(list) 并将其内容替换为另一个 vector(templist) 的内容,并在完成后清除 templist。这是正确的代码吗?

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

最佳答案

这就是 swap 方法的用途:

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

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

关于c++ - 清除 vector 并让一个 vector 等于另一个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com