Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext
indexed
indexed example

Syntax

Code

Pipe

rng | boost::adaptors::indexed(start_index)

Function

boost::adaptors::index(rng, start_index)

#include <boost/range/adaptor/indexed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/assign.hpp>
#include <iterator>
#include <iostream>
#include <vector>

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));
}

int main(int argc, const char* argv[])
{
    using namespace boost::assign;
    using namespace boost::adaptors;

    std::vector<int> input;
    input += 10,20,30,40,50,60,70,80,90;

    display_element_and_index( input | indexed(0) );

    return 0;
}

This would produce the output:

Element = 10 Index = 0
Element = 20 Index = 1
Element = 30 Index = 2
Element = 40 Index = 3
Element = 50 Index = 4
Element = 60 Index = 5
Element = 70 Index = 6
Element = 80 Index = 7
Element = 90 Index = 8


PrevUpHomeNext