C++ std::includes() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::includes() tells you if every element of the second sorted range is also in the first sorted range. It is a subset test.

Both ranges must be sorted with the same order.

Syntax

Add #include <algorithm>, then call std::includes(). Start with these two lines.

C++
std::includes(a.begin(), a.end(), b.begin(), b.end());
std::includes(a.begin(), a.end(), b.begin(), b.end(), comp);
CallWhat it does
std::includes(a.begin(), a.end(), b.begin(), b.end());Does a contain every element of b?
std::includes(a.begin(), a.end(), b.begin(), b.end(), comp);Use your own ordering.
Library declarations
C++
template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2);

Parameters

ParameterDescription
first1, last1Required. The sorted range to search in.
first2, last2Required. The sorted range of elements to look for.
compOptional. The comparison both ranges were sorted with.

Return Value

Return ValueDescription
trueEvery element of the second range is in the first range (or the second range is empty).
falseAt least one element of the second range is missing.

C++ Examples

Example 1 — A subset

Check that 2 and 4 are inside a bigger list.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b = {2, 4};

    bool ok = std::includes(a.begin(), a.end(), b.begin(), b.end());

    std::cout << std::boolalpha << "Contains all: " << ok << '\n';
    return 0;
}

How It Works

  1. Both vectors are sorted, which includes() needs.
  2. It walks through a and finds 2, then 4.
  3. Every value of b was found, so the result is true.

Example 2 — A missing element

One missing element makes the answer false.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 3, 4, 5};
    std::vector<int> b = {2, 6};

    bool ok = std::includes(a.begin(), a.end(), b.begin(), b.end());

    std::cout << std::boolalpha << "Contains all: " << ok << '\n';
    return 0;
}

How It Works

  1. 2 is found in a, but 6 is not.
  2. After 5 the first range ends, and 6 is still waiting to be matched.
  3. The answer is false.

Example 3 — Letters in a word

Check that some letters appear in a sorted word.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string text = "abcdef";
    std::string part = "ace";

    bool ok = std::includes(text.begin(), text.end(), part.begin(), part.end());

    std::cout << '"' << part << "\" is inside: " << std::boolalpha << ok << '\n';
    return 0;
}

How It Works

  1. Both strings are in alphabetical order.
  2. a, c and e all appear in abcdef, in the same order.
  3. So includes() returns true.

Frequently Asked Questions

In C++, std::includes() checks whether a sorted range contains all elements of another sorted range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns true if the first sorted range contains every element of the second sorted range.
Yes. includes() relies on both ranges being sorted with the same order.
No. includes() checks that all elements of a sorted range are present, as a subset. It is not a substring search.

Next: inplace_merge()

Merges two consecutive sorted parts of one range in place.

inplace_merge() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful