writable zip ranges are not possible?

那年仲夏 提交于 2019-12-11 11:05:08

问题


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:


  1. copy takes an output iterator, not an output range. So you need to call begin on the zip view and turn it into an iterator.
  2. With that fixed, you run into a separate problem. zipping two ranges produce a pair (well, a common_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 make data a vector of pairs, then it would work.


来源:https://stackoverflow.com/questions/54858938/writable-zip-ranges-are-not-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!