C++

C++ Includes()

The C++ STL is a potent collection of C++ template classes that offer classes and methods with templates that implement many well-known and widely used algorithms and data structures including vectors, lists, queues, and stacks. Among them, the STL method includes() is highly useful for determining whether one sorted range includes another. In other words, given that the set is ordered, it is useful to determine if a collection is a subset of another set or not. Both the range and the set must be assembled in the same way, both in ascending and descending order. It won’t be able to identify otherwise. Utilizing the includes() function from the “algorithm” header accomplishes this operation. In this article, we will examine the purpose of the std::includes() and how to effectively use it in a program.

Example 1: Program of Includes() Method Using “<” Operator

The program demonstrates the working of the C++ includes() method. Before this, we need to understand the syntax which is given in the following:

# bool includes (input_iter1 begin_1, input_iter2 end_1, input_iter2 begin_2, input_iter2 end_2,)

The detail of each argument passed inside the includes() method is as follows:

input_iter1 begin_1: Begin by giving the iterator the beginning location in the sorted sequence.

input_iter1 end_1: Move the given iterator to the last point in the first ordered sequence.

input_iter2 begin_2: Begin the second ordered series by passing an iterator to the first position.

input_iter2 end_2: Move the given iterator to the last point in the second ordered sequence.

Return: If each component of the second container is contained in the first container, the return statement is true. It is linearly complex in time.

The implementation begins by including the C++ library that supports this program’s functions. Then, we set the main method for this program. In the main code, we defined our first array of vectors as “array_1”. Also, we initialized the second array of vectors as “array_2”. After that, we have a sort algorithm to sort these two vector arrays. Next, we used the includes() method with the if-else condition. With this, we checked whether all the elements in the vector array_2 lie in the vector array_1. One of the cout statements from the “if” condition and from the “else” condition are printed on the condition results.

As all the elements of the vector array_2 are present in the vector array_1, the first cout statement is printed as follows:

Example 2: Program of Includes() Method Using Default Comparator

The includes() function can be extended by using our custom comparator. The includes() method syntax is given as follows:

# bool includes (input_iter1 begin_1, input_iter2 end_1, input_iter2 begin_2, input_iter2 end_2, Compare comparator)

Now that the syntax has been provided, the details of each input in the includes() method are discussed in the following:

input_iter1 begin_1: Begin by giving the iterator the first position in the first sorted sequence.

input_iter2 end_1: Advance the argument iterator to the last spot in the first ordered sequence.

input_iter2 begin_2: Input the iterator to the second sorted sequence’s starting place at begin_2.

input_iter2 end_2: Input the iterator to position 2 of the second ordered sequence.

Compare comparator: Comparator function that evaluates each element to a Boolean value of true or false. Two arguments are accepted by this function. This cannot change the values and can be either a function pointer or a function object.

Return: The True value is returned if each component of the second container fits inside the first container.

We included the main libraries of C++. Then, we created the comparator function which declares the variables “i” and “j” of the data type “int”. The function has the condition “i<j”. Then, we entered the main method where we initialized the two vector arrays as “Arr1” and “Arr2”. We sorted these initialized vector arrays by applying the sort method to them. After that, we invoked the if-else condition with the includes() method. Inside the includes() method, we passed the begin iterator and end iterator for both the vector arrays to check if the elements of the “Arr2” lie in “Arr1”. If they do exist, the if cout statement is executed. Otherwise, the cout statement in the else condition is executed.

In the prompt, the if cout statement is printed that the element in the “Arr2” lies in the “Arr1”.

Example 3: Program of Includes() Method Using the Real Application

A person wins the lottery if all of the numbers on his ticket are drawn. This function can be used in a system where this is the case to identify the lottery winner or to determine whether one set is a subset of another. The code explanation for the latter is provided in the following:

In the program, we imported the library and std using the namespace. Then, we set the main method for this program. Inside the method, we declared the two variables, “lottery_no” and “card_nums”, of the vector class which has type int. Then, we initialized these variables with a set of numerical vectors. After that, these initial vector arrays are sorted with the sort method. Then, we called the if-else condition for the includes() method operation. They include being called inside the if condition to verify that all the numbers on the card are present in the lottery.

As you can see, the output generated shows that all the elements of the card are present in the lottery set.

Conclusion

This explained the fundamentals of utilizing the includes() method to find the matched components. The STL method includes() is highly useful for determining whether one sorted range includes another. Here, we covered three examples of the includes() methods in C++. First, we demonstrated the two types of usage of the includes() method with their syntax and example program. In the third example, we explained the functionality of the includes() method by taking a real-life scenario.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.