C++,STL。从矢量中删除具体值<string>?
c++, stl. Remove a concrete value from vector<string>?
例如:
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框架?
有一个非常好的删除习惯用法:
#include <algorithm>
strs.erase( std::remove(strs.begin(), strs.end(), std::string("3")), strs.end() );
Scott Meyers在他的《有效的STL:改进标准模板库使用的50种具体方法》中谈到了"删除"习语。它似乎非常适合您的情况:
#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() );
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 为std::string的某个索引赋值
- <<操作员在下面的行中工作
- std中有类似find_last_of的函数,而string中没有
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数