Dimfred
Dimfred

Reputation: 312

Is there a standard way to iterate over a range of iterators with a range based for loop without using the iterator syntax

I have a range = pair<iterator, iterator>, which I receive from boost::multi_index_container::equal_range I would like to iterate over the range with a range based for loop

for( auto element : range ) {
//do
}

I can solve this problem by implementing a class which receives the iterators as an input like this:

template< typename T >
struct merge
{
   merge(T begin, T end) : _begin(begin), _end(end) {}
   T _begin;
   T _end;

   T begin() {
      return _begin;
   }

   T end() {
      return _end;
   }
};

int main()
{
   vector<int> v = {1, 2, 3};

   for(auto& i : merge(v.begin(), v.end())) {
      std::cout << i << std::endl;
   }
}

I know I could just iterate over the vector itself here.

Is there a standard / better way of doing this?

Upvotes: 0

Views: 62

Answers (1)

Dimfred
Dimfred

Reputation: 312

Okay found it 30 seconds after posting the question...

boost::make_iterator_range(begin, end) seems the way to go

Upvotes: 3

Related Questions