例如:
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框架?
有一个非常好的Erase-remove 习惯用法:
#include <algorithm>
strs.erase( std::remove(strs.begin(), strs.end(), std::string("3")), strs.end() );
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() );