可能是重复问题:
remove_if等效于std::map吗?
我有一组字符串:
set <wstring> strings;
// ...
我希望能够根据谓词(predicate)删除字符串,例如:
std::remove_if ( strings.begin(), strings.end(), []( const wstring &s ) -> bool { return s == L"matching"; });
尝试此操作时,我收到以下编译器错误:
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm(1840): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>'
这个错误似乎表明std::string
没有按值复制构造函数(这是不合法的)。
使用std::remove_if
和std::set
是否有问题?我应该做些其他事情,比如多次迭代set::find()
然后再调用set::erase()
吗?
strings.erase(L"matching");
替换。人们会认为你实际的谓词不是那么琐碎。 - Robᵩ