问题
The following is failing:
#include <range/v3/view.hpp>
#include <range/v3/view/zip.hpp>
#include <range/v3/utility/iterator.hpp>
// ...
std::vector< std::tuple<int, std::string> > const data{
{1,"a"},
{2,"b"},
{3,"c"}
};
std::vector<int> vi(data.size());
std::vector<std::string> vs(data.size());
using namespace ranges;
copy(data, view::zip(vi,vs) ); // error
clang says
No matching function for call to object of type 'const
ranges::v3::with_braced_init_args<ranges::v3::copy_fn>'
Assuming this is by design, why?
And, how can I do this obvious thing with ranges?
回答1:
copy
takes an output iterator, not an output range. So you need to callbegin
on the zip view and turn it into an iterator.- With that fixed, you run into a separate problem.
zip
ping two ranges produce apair
(well, acommon_pair
), but while tuples of two elements are assignable from pairs, pairs are not assignable from tuples of two elements. As a result, we can't do the equivalent of*zip_iterator = *data.begin()
, and the concept check fails. If you makedata
a vector ofpair
s, then it would work.
来源:https://stackoverflow.com/questions/54858938/writable-zip-ranges-are-not-possible