Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::includes

 

std::includes is an STL algorithm to determine if one sorted container includes all values of another sorted container.

 

#include <algorithm>
#include <cassert>
#include <set>

int main()
{
  //Create a std::set (which is always sorted)
  std::set<int> s;
  s.insert(1);
  s.insert(2);
  s.insert(3);
  s.insert(5);
  s.insert(7);

  std::set<int> t(s);

  //Assume t has at least all elements of s
  assert(std::includes(t.begin(),t.end(),s.begin(),s.end()) == true);
  //Assume s has at least all elements of t
  assert(std::includes(s.begin(),s.end(),t.begin(),t.end()) == true);

  t.insert(4);

  //Again assume t has at least all elements of s
  assert(std::includes(t.begin(),t.end(),s.begin(),s.end()) == true);
  //Assume s does not have at least all elements of t anymore
  assert(std::includes(s.begin(),s.end(),t.begin(),t.end()) == false);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict