Home » C++ algorithm includes() function

C++ algorithm includes() function

by Online Tutorials Library

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

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():

Output:

true  false  

Example 2

Let’s see another simple example:

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:

Output:

container includes continent!  container includes continent!  

Example 4

Let’s see a simple example:

Output:

User has won lottery ( all numbers are lottey numbers )  

Next TopicC++ Algorithm

You may also like