【发布时间】:2014-05-02 05:05:37
【问题描述】:
所以我试图将向量 b 中的元素附加到向量 a 的末尾,同时擦除向量 b 中的所有内容。下面是我的代码,由于某种原因,擦除无法正常工作。感谢您的任何意见谢谢!!
void problem3(std::vector<int>& a, std::vector<int>& b){
typedef std::vector<int>::iterator iter;
int place_holder;
for (iter i = b.begin();i !=b.end();i++){
place_holder = *i;//use place hodler to store values temporairly
a.push_back(place_holder);//erase the elements from b
b.erase(i);
//std::cout<<b.size()<<'\n';
//append at the end of a
}
}
【问题讨论】:
-
一个更好的主意是从循环中取出
b.erase(i),然后在循环结束后擦除所有元素