TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm includes() function

C++ algorithm includes() function with c++ tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm includes()

C++ Algorithm includes() function returns true if every element from the sorted range [first2, last2) is found within the sorted range [first1, last1).

It also returns true if [first2, last2) is empty.

Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version.

Syntax

template <class InputIterator1, class InputIterator2>
  bool includes ( InputIterator1 first1, InputIterator1 last1,
                  InputIterator2 first2, InputIterator2 last2);

template <class InputIterator1, class InputIterator2, class Compare>
  bool includes ( InputIterator1 first1, InputIterator1 last1,
                  InputIterator2 first2, InputIterator2 last2, Compare comp);

Parameter

first1: An input iterator pointing to the first element in the first of two sorted source sequence to be tested for whether all the elements of the second are occupied in the first.

last1: An input iterator pointing to the past last element in the first of two sorted source sequence to be tested for whether all the elements of second are contained in the first.

first2: An input iterator pointing to the first element in the second sorted source sequence to be tested for whether all the elements of second are contained in the first.

last2: An input-iterator pointing to the past last element in the second sorted source sequence to be tested for whether all the elements of the second are contained in the first.

comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements.

Return value

This function returns true if every element from [first2, last2) is a member of [first1, last1), otherwise it returns false.

Complexity

Complexity is linear in the distance between [first1, last1) and [first2, last2): performs up to 2*(count1+count2)-1 comparisons. Where count1 = last1- first1 and count2 = last2- first2.

Data races

The object in the range [first1, last1) and [first2. last2) are accessed.

Exceptions

This function throws an exception if any of element comparisons or an operation on iterator throws an exception.

Note: The invalid parameters cause an undefined behavior.

Example 1

Let's see the simple example to demonstrate the use of includes():

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
  set<int> a = {0, 2, 3, 4, 5, 6};
  set<int> b = {2, 4, 6};
  set<int> c = {2, 4, 7};

  cout << boolalpha;

  cout << includes(a.begin(), a.end(), b.begin(), b.end()) << endl;
  cout << includes(a.begin(), a.end(), c.begin(), c.end()) << endl;
  
  return 0;
}

Output:

true
false

Example 2

Let's see another simple example:

#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>

using namespace std;
 
int main()
{
  vector<char> v1 {'a', 'b', 'c', 'f', 'h', 'x'};
  vector<char> v2 {'a', 'b', 'c'};
  vector<char> v3 {'a', 'c'};
  vector<char> v4 {'g'};
  vector<char> v5 {'a', 'c', 'g'};
 
  for (auto i : v1) cout << i << ' ';
  cout << "\nincludes:\n" << boolalpha;
 
  for (auto i : v2) cout << i << ' ';
  cout << ": " << includes(v1.begin(), v1.end(), v2.begin(), v2.end()) << '\n';
  for (auto i : v3) cout << i << ' ';
  cout << ": " << includes(v1.begin(), v1.end(), v3.begin(), v3.end()) << '\n';
  for (auto i : v4) cout << i << ' ';
  cout << ": " << includes(v1.begin(), v1.end(), v4.begin(), v4.end()) << '\n';
  for (auto i : v5) cout << i << ' ';
  cout << ": " << includes(v1.begin(), v1.end(), v5.begin(), v5.end()) << '\n';
 
  auto cmp_nocase = [](char a, char b) {
    return std::tolower(a) < std::tolower(b);
  };
 
  vector<char> v6 {'A', 'B', 'C'};
  for (auto i : v6) cout << i << ' ';
  cout << ": (case-insensitive) "
            << includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase)
            << '\n';
            
  return 0;
}

Output:

a b c f h x 
includes:
a b c : true
a c : true
g : false
a c g : false
A B C : (case-insensitive) true

Example 3

Let's see another simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::includes, std::sort

using namespace std;

bool myfunction (int i, int j) { return i<j; }

int main () {
  int container[] = {5,10,15,20,25,30,35,40,45,50};
  int continent[] = {40,30,20,10};

  sort (container,container+10);
  sort (continent,continent+4);

  // using default comparison:
  if ( includes(container,container+10,continent,continent+4) )
    cout << "container includes continent!\n";

  // using myfunction as comp:
  if ( includes(container,container+10,continent,continent+4, myfunction) )
    cout << "container includes continent!\n";

  return 0;
}

Output:

container includes continent!
container includes continent!

Example 4

Let's see a simple example:

#include <iostream>     // std::cout
#include <algorithm>    // std::includes, std::sort

using namespace std; 
  
int main() 
{ 
    // lottery numbers 
    vector<int> lottery = { 1, 4, 6, 3, 2, 54 , 32 }; 
      
    // Numbers in user's card 
    vector<int> user = { 1, 2, 4, 6 }; 
      
    // sorting initial containers 
    sort(lottery.begin(), lottery.end()); 
    sort(user.begin(), user.end()); 
      
    // using include() check if all elements  
    // of user are present as lottery numbers 
    if(includes(lottery.begin(), lottery.end(), user.begin(), user.end())) 
    cout << "User has won lottery ( all numbers are lottey numbers )"; 
    else 
    cout << "User has not won the lottery"; 
    
    return 0;    
}

Output:

User has won lottery ( all numbers are lottey numbers )

Next TopicC++ Algorithm




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf