boost zip_iterator and std::sort

前端 未结 4 1874
北荒
北荒 2020-11-29 10:14

I have two arrays values and keys both of the same length. I want to sort-by-key the values array using the keys array a

4条回答
  •  無奈伤痛
    2020-11-29 11:05

    You can't sort a pair of zip_iterators.

    Firstly, make_zip_iterator takes a tuple of iterators as input, so you could call:

    boost::make_zip_iterator(boost::make_tuple( ... ))
    

    but that won't compile either, because keys and keys+N doesn't have the same type. We need to force keys to become a pointer:

    std::sort(boost::make_zip_iterator(boost::make_tuple(+keys, +values)),
              boost::make_zip_iterator(boost::make_tuple(keys+N, values+N)));
    

    this will compile, but the sorted result is still wrong, because a zip_iterator only models a Readable iterator, but std::sort also needs the input to be Writable as described here, so you can't sort using zip_iterator.

提交回复
热议问题