change the return type from the indexed adaptor so that the index is available from the range element.

This commit is contained in:
Neil Groves
2014-06-09 02:12:00 +01:00
parent 344f578934
commit 5ce6dc7814
5 changed files with 545 additions and 223 deletions

View File

@ -9,8 +9,12 @@
// For more information, see http://www.boost.org/libs/range/
//
//[indexed_example
//<-
#include <boost/config.hpp>
//->
#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
@ -20,8 +24,6 @@
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/range/algorithm_ext/push_back.hpp>
namespace
{
@ -35,14 +37,14 @@ void check_element_and_index(
BOOST_CHECK_EQUAL( std::distance(test_first, test_last),
std::distance(reference_first, reference_last) );
int reference_index = 0;
std::ptrdiff_t reference_index = 0;
Iterator1 test_it = test_first;
Iterator2 reference_it = reference_first;
for (; test_it != test_last; ++test_it, ++reference_it, ++reference_index)
{
BOOST_CHECK_EQUAL( *test_it, *reference_it );
BOOST_CHECK_EQUAL( test_it.index(), reference_index );
BOOST_CHECK_EQUAL(test_it->value(), *reference_it);
BOOST_CHECK_EQUAL(test_it->index(), reference_index);
}
}
@ -51,24 +53,11 @@ void check_element_and_index(
const SinglePassRange1& test_rng,
const SinglePassRange2& reference_rng)
{
check_element_and_index(boost::begin(test_rng), boost::end(test_rng),
check_element_and_index(
boost::begin(test_rng), boost::end(test_rng),
boost::begin(reference_rng), boost::end(reference_rng));
}
//->
template<class Iterator>
void display_element_and_index(Iterator first, Iterator last)
{
for (Iterator it = first; it != last; ++it)
{
std::cout << "Element = " << *it << " Index = " << it.index() << std::endl;
}
}
template<class SinglePassRange>
void display_element_and_index(const SinglePassRange& rng)
{
display_element_and_index(boost::begin(rng), boost::end(rng));
}
//->
//<-
void indexed_example_test()
@ -81,8 +70,19 @@ void indexed_example_test()
std::vector<int> input;
input += 10,20,30,40,50,60,70,80,90;
display_element_and_index( input | indexed(0) );
//<-
#ifndef BOOST_NO_CXX11_RANGE_BASED_FOR
//->
for (const auto& element : input | indexed(0))
{
std::cout << "Element = " << element.value()
<< " Index = " << element.index()
<< std::endl;
}
//<-
#endif // C++11 has range for loop
//->
//= return 0;
//=}
//]