我如何生成一个输出 vector ,它根据输入 vector 是否以某个子字符串开头来过滤输入 vector 。我正在使用 c++98 和 boost。
据我所知:
std::string stringToFilterBy("2");
std::vector<std::string> input = boost::assign::list_of("1")("2")("22")("33")("222");
std::vector<int> output;
boost::copy( input | boost::adaptors::filtered(boost::starts_with), std::back_inserter(output) );
最佳答案
您可以使用 std::remove_copy_if相反:
#include <algorithm>
#include <iterator>
#include <vector>
struct filter : public std::unary_function<std::string, bool> {
filter(const std::string &by) : by_(by) {}
bool operator()(const std::string &s) const {
return s.find(by_) == 0;
}
std::string by_;
};
std::vector<std::string> in, out;
std::remove_copy_if(in.begin(), in.end(), std::back_inserter(out),
std::not1(filter("2")));
关于c++ - 过滤 std::string 的 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13492025/