0

例如:

vector<string> strs;
strs.push_back("1");
strs.push_back("2");
strs.push_back("3");
strs.push_back("4");
strs.push_back("3");

//strs.removeAllOccurencesOfValue("3");

我找到了这个例子:

关联

但是有没有更简单的方法?例如使用boost框架?

4

2 回答 2

4

有一个非常好的Erase-remove 习惯用法

#include <algorithm>

strs.erase( std::remove(strs.begin(), strs.end(), std::string("3")), strs.end() );
于 2013-08-30T13:00:23.533 回答
1

Scott Meyers在其Effective STL: 50 Specific Ways to Improvement Your Use of the Standard Template Library中谈到了Erase-remove 习惯用法。它似乎非常适合您的情况:

#include <algorithm>    // for std::remove

vector<string> strs;
strs.push_back("1");
strs.push_back("2");
strs.push_back("3");
strs.push_back("4");
strs.push_back("3");

strs.erase( std::remove( strs.begin(), strs.end(), "3" ), strs.end() );
于 2013-08-30T13:03:51.127 回答