Llopeth
Llopeth

Reputation: 406

Boost range adaptors

When using range adaptors in a find algorithm, I have to repeat all the adaptor chain to get the corresponding end() iterator. e.g:

std::vector<size_t> numbers = { 10, 11, 12, 13, 14, 2, 1, 3,3,50, 55} ;

if ( find(numbers|reversed,99) != (numbers|reversed).end() )
                                 //^adaptor chain repeated
{
    std::cout << "FOUND!!!!!" << std::endl;
}

Is there a way to get the corresponding end iterator without having to repeat it again?

Upvotes: 0

Views: 232

Answers (1)

Thomas
Thomas

Reputation: 181735

Too bad that find calls often end up verbose – it's even worse with STL than with Boost ranges. You could introduce a little helper function:

template<class SinglePassRange, class Value>
bool contains(SinglePassRange& rng, Value const &val) {
  return boost::find(rng, val) != rng.end();
}

I didn't test this so it probably doesn't compile, but you get the idea.

Upvotes: 1

Related Questions