Created
April 25, 2017 17:25
-
-
Save jefftrull/f5d8ecb218815523f5d7af836eea8b9a to your computer and use it in GitHub Desktop.
Cannot sort with boost::iterator::zip_iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// experimenting with zip iterator and sort | |
#include <vector> | |
#include <iostream> | |
#include <boost/iterator/zip_iterator.hpp> | |
#include <boost/fusion/adapted/std_tuple.hpp> | |
using zip_it = boost::zip_iterator<std::tuple<std::vector<std::size_t>::iterator, | |
std::vector<std::size_t>::iterator, | |
std::vector<double>::iterator>>; | |
namespace std { | |
template<typename IndexT, typename NumericT> | |
void swap(typename std::tuple<IndexT&, IndexT&, NumericT&> a, | |
typename std::tuple<IndexT&, IndexT&, NumericT&> b) | |
{ | |
std::cerr << "initial values: A={" << std::get<0>(a) << ", " << std::get<1>(a) << ", " << std::get<2>(a) << "} "; | |
std::cerr << "B={" << std::get<0>(b) << ", " << std::get<1>(b) << ", " << std::get<2>(b) << "}\n"; | |
using std::swap; | |
swap(std::get<0>(a), std::get<0>(b)); | |
swap(std::get<1>(a), std::get<1>(b)); | |
swap(std::get<2>(a), std::get<2>(b)); | |
std::cerr << "final values: A={" << std::get<0>(a) << ", " << std::get<1>(a) << ", " << std::get<2>(a) << "} "; | |
std::cerr << "B={" << std::get<0>(b) << ", " << std::get<1>(b) << ", " << std::get<2>(b) << "}" << std::endl; | |
} | |
} | |
int main() { | |
std::vector<std::size_t> rows{3, 2, 1}; | |
std::vector<std::size_t> cols{1, 2, 3}; | |
std::vector<double> values{4.0, 5.0, 6.0}; | |
auto start = boost::make_zip_iterator(std::make_tuple(rows.begin(), cols.begin(), values.begin())); | |
auto stop = boost::make_zip_iterator(std::make_tuple(rows.end(), cols.end(), values.end())); | |
std::sort(start, stop); | |
for ( int i = 0; i < 3; ++i ) { | |
std::cerr << rows[i] << ", " << cols[i] << ", " << values[i] << "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment