c++ - 过滤 std::string 的 std::vector

标签 c++ boost c++98

我如何生成一个输出 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/

相关文章:

c++ - 类和函数同名?

c# - 盲目地将 bytearray 转换为结构?

c++ - boost 字符串拆分以消除单词中的空格

C++ - 如何以平台无关、线程安全的方式将文件的上次修改日期和时间格式化为用户首选的日期/时间区域设置格式

c++ - 从 Boost::Interprocess 中删除 RTTI

c++ - C++98如何处理多个不同类型的参数?

c++ - 同时支持C++98和C++11

c++ - 我什么时候应该按值返回,而不是返回一个唯一的指针

c++ - ABI 规范中的内存布局是否仅适用于 ABI 边界?

c++ - 是否可以为所有子类创建一个实例?