一种方法是:
#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));
}