我正在使用gcc 9.2.0和boost 1.55。
我有2个向量:
vector< pair< string, int > > source;
vector< string > dest;
我需要将源向量转换为dest,该目标应仅包含源向量的字符串元素。
使用boost :: push_back和adapters是否可以实现?
boost::range::push_back( dest, source | ?adaptor? );
目前,我有这个可行的代码,但是应该更改它:
transform( source.begin(), source.end(), back_inserter(dest), __gnu_cxx::select1st< std::pair< std::string, int > >() );
继续发布最小改进的趋势:
#include <boost/range/adaptor/map.hpp>
#include <fmt/ranges.h>
using namespace std::string_literals;
using namespace boost::adaptors;
template <typename R>
auto to_vector(R const& range) {
return std::vector(boost::begin(range), boost::end(range));
}
int main() {
std::vector source {std::pair {"one"s,1},{"two",2},{"three",3}};
fmt::print("keys {}\nvalues {}\n",
source | map_keys,
source | map_values);
// if you really need the vectors
auto keys = to_vector(source | map_keys);
auto values = to_vector(source | map_values);
}
版画
keys {"one", "two", "three"}
values {1, 2, 3}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句