2

I try to understand the following example given here:

template<class SinglePassRange1, class SinglePassRange2>
std::pair <
    typename range_iterator<SinglePassRange1>::type,
    typename range_iterator<const SinglePassRange2>::type
>
mismatch(SinglePassRange1& rng1, const SinglePassRange2& rng2);

And here is the description:

mismatch finds the first position where the correseponding elements from the two ranges rng1 and rng2 are not equal.

The main thing that is not clear to me in the above example is: What is given as an input and what is an output of the mismatch function?

rng1 and rng2 are object of classes SinglePassRange1 and SinglePassRange2, respectively. But what are these classes? Where are they defined? They are supposed to be "ranges" but what are "ranges"?

It is also not clear to me what all these lines before the calling of the mismatch function do. The first line looks like we are going to define a class template, but we do not do it later.

didierc
  • 14,572
  • 3
  • 32
  • 52
Roman
  • 124,451
  • 167
  • 349
  • 456

2 Answers2

2

A range is a pair of iterators operating on the same container. A SinglePassRange is such a range where iterators are single pass iterators, ie. iterators which are comparable and incrementable. By overloading of the mismatch function, the parameters may also be containers supporting that type of range iterators, in which case the begin() and end() iterators of the container are used.

The returned value is the first pair of iterators - one of each range - which don't satisfy the equality predicate. That pair is what the first couple of lines decribes after the template parameter declaration.

std::pair <
    typename range_iterator<SinglePassRange1>::type,
    typename range_iterator<const SinglePassRange2>::type
>

As you can see, each argument is the type of the range iterator of each range type.

The keyword typename before arguments is necessary, because both are actually dependent names, ie. types that are defined within other types, which are themselves dependent on the outer template parameters (SinglePassRange1 and 2).

The mismatch function is probably similar to the following (types elided):

 auto mismatch(auto &rng1, auto &rng2){
     auto it1 = rng1.first, it2 = rng2.first;
     while (it1 != rng1.second && it2 != rng2.second)
          if (it1 != it2)
              break;
          else { ++it1; ++it2; }
     return make_pair(it1, it2);  
 }
Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52
1

It is also not clear to me what all these lines before the calling of the mismatch function do. The first line looks like we are going to define a class template, but we do not do it later.

This is not an example. It is a declaration of a function template, which is part of the boost range library.

The main thing that is not clear to me in the above example is: What is given as an input and what is an output of the mismatch function?

As documented further below on the page you link, the input must have a type that is a model of the Single Pass Range concept.

More information on boost ranges ist available at http://www.boost.org/doc/libs/1_50_0_beta1/libs/range/doc/html/range/introduction.html

As mentioned there, the most common use is to pass in standard containers. Built-in arrays also work. And boost::iterator_range allows you to use any pair of (forward) iterators that form a range.

So you could have

vector<string> vec { ... };
list<string> lis { ... };

auto result = mismatch(vec, lis);

Here result will have type pair<boost::range_iterator<vector<string>>::type, boost::range_iterator<list<string>>::type>

JoergB
  • 4,383
  • 21
  • 19