std::includes correction

The sample implementation given at http://www.cplusplus.com/reference/algorithm/includes/ is incorrect. It should return true when both ranges are empty, but as written would return false.

For example,
1
2
3
std::set<int> a;
std::set<int> b;
bool test = std::includes(a.begin(), a.end(), b.begin(), b.end());


Suggested replacement:

1
2
3
4
5
6
7
8
9
10
11
12
template <class InputIterator1, class InputIterator2>
  bool includes ( InputIterator1 first1, InputIterator1 last1,
                  InputIterator2 first2, InputIterator2 last2 )
{
  while (first2!=last2)
  {
    if (first1==last1 || *first2<*first1) return false;
    else if (*first1<*first2) ++first1;
    else { ++first1; ++first2; }
  }
  return true;
}

Topic archived. No new replies allowed.