C++ algorithm includes() function
Example
Find out if the values 2, 3 and 5 exist in a vector:
vector<int> numbers = {1, 7, 3, 5, 3, 9, 2};
vector<int> search = {2, 3, 5};
// Sort the numbers vector
sort(numbers.begin(), numbers.end());
// Check if search vector is included in numbers
if (includes(numbers.begin(), numbers.end(), search.begin(), search.end())) {
cout << "Found";
} else {
cout << "Not found";
}
Try it Yourself »
Definition and Usage
The includes()
function tests a data range to check if all of the values from another data range can be found. It returns a boolean value 1 if the values can be found, it returns 0 otherwise.
Both data ranges must already be sorted. If they are not sorted then the function may return an incorrect result.
The data ranges are specified by iterators.
Syntax
includes(iterator start, iterator end, iterator values_start, iterator values_end);
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range being tested. |
end | Required. An iterator pointing to the end of the data range being tested. Elements up to this position will be tested, but the element at this position will not be included. |
values_start | Required. An iterator pointing to the start of a data range containing the values. |
values_end | Required. An iterator pointing to the end of a data range containing the values. |
Technical Details
Returns: | A boolean value:
|
---|
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.
Read more about booleans in our Booleans Tutorial.