过滤std :: string的std :: vector

时间:2012-11-21 11:12:33

标签: c++ boost c++98

如何生成输出向量,根据输入向量是否以某个子字符串开头来过滤输入向量。我正在使用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) );

2 个答案:

答案 0 :(得分:4)

您可以改为使用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")));

答案 1 :(得分:4)

一种方法是:

#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <iterator>
#include <vector>

int main() {
    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::bind(
                boost::starts_with<std::string,std::string>, _1, stringToFilterBy))
      | boost::adaptors::transformed(boost::lexical_cast<int, std::string>),
        std::back_inserter(output));
}

或没有boost::bind(这样可以避免命名类型并避免使用函数指针,但需要添加一些一次性结构):

#include <boost/algorithm/string/predicate.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/assign.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
#include <iterator>
#include <vector>

template<typename Range1T>
struct StartsWith_T {
    StartsWith_T(Range1T const& test) : test(&test) {}
    template<typename Range2T>
    bool operator()(Range2T const& input) const {
        return boost::starts_with(input, *test);
    }
    Range1T const* test;
};

template<typename Range1T>
StartsWith_T<Range1T> StartsWith(Range1T const& test) {
    return StartsWith_T<Range1T>(test);
}

template<typename T>
struct LexicalCaster {
    typedef T result_type;
    template<typename Input>
    T operator()(Input const& input) const {
        return boost::lexical_cast<T>(input);
    }
};

int main() {
    std::string stringToFilterBy("2");
    std::vector<std::string> input =
        boost::assign::list_of("1")("2")("22")("33")("222");
    std::vector<int> output;

    using namespace boost::adaptors;
    boost::copy(
        input
      | filtered(StartsWith(stringToFilterBy))
      | transformed(LexicalCaster<int>()),
        std::back_inserter(output));
}