Using std :: get and std :: binding to boot tuples, zip_iterator, etc.

What are my options for using std::get<>() and std::tie<>() along with boost constructs?

Example: I want to use a range-based for-loop to iterate over multiple containers. I can implement a zip function that uses boost::zip_iterator .

  #include <boost/iterator/zip_iterator.hpp> #include <boost/range.hpp> template <typename... TContainer> auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>> { auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...)); auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...)); return boost::make_iterator_range(zip_begin, zip_end); } 

Now I can use it as follows:

 std:list<int> a; std::vector<double> b; ... for (auto t : zip(a, b)) { // access elements by boost::get<0>(t), boost::get<1>(t) // or use boost::tie(a_element, b_element) } 

Problems arise when I call some other method that expects std::tuple or std::pair - I need to convert), because the rest of the code uses std::tuples , or when std::get<>() used in the template std::get<>() and / or std::tie() .

I found some fixes that add std::tuple support for zip_iterator , but they do not apply in my version (I use Boost 1.54).

Am I missing something? What are my options to force zip_iterator to return std::tuple or to do std::get , std::tie , etc. Available for boost types?

+7
c ++ boost c ++ 11 stl tuples
source share
2 answers

You tried

 #include <boost/iterator/zip_iterator.hpp> #include <boost/range.hpp> #include <tuple> template <typename... TContainer> auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(std::make_tuple(std::begin(containers)...))>> { ^^^ auto zip_begin = boost::make_zip_iterator(std::make_tuple(std::begin(containers)...)); ^^^ auto zip_end = boost::make_zip_iterator(std::make_tuple(std::end(containers)...)); ^^^ return boost::make_iterator_range(zip_begin, zip_end); } 
0
source share

use boost::combine in boost> = 1.55.

for (auto a_tuple: boost::combine(va, vb)) {...} or

BOOST_FOREACH(boost::tie(elem_a, elem_b), boost::combine(va, vb)) {...}

0
source share

All Articles