【发布时间】:2012-11-21 11:12:33
【问题描述】:
如何生成一个输出向量,它根据输入向量是否以某个子字符串开头来过滤它。我正在使用 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) );
【问题讨论】:
-
你可以使用
regex作为过滤器,它是boost & c++11的一部分 -
@Haocheng 但是,starts_with 谓词不是比正则表达式更有效吗?
-
现在无法测试,但我认为您希望
boost::bind(boost::starts_with<std::string,std::string>(), _1, stringToFilterBy)作为您的谓词。所以当过滤器适配器调用带有元素x的谓词时,它调用boost::starts_with(x, stringToFilterBy)。 -
@Baz 是的...我刚才没听懂你的意思
-
@Steve Jessop boost::copy( 输入 | boost::bind(boost::starts_with<:string>(), _1, stringToFilterBy), std::back_inserter (输出) );这不编译。编译器抱怨starts_with 不接受零参数。