We will learn how to check if a vector contains all elements of another vector at any position. Basically, we will check if a vector is a subset of another vector or not in C++.

Introduction

Suppose we have two vectors i.e.

std::vector<int> firstVec = { 11, 12, 13, 17, 19, 10};
std::vector<int> secondVec = { 19, 13, 12};

Now we want to check if the first vector contains all the elements of the second vector or not, irrespective of any position. There are different ways to do this. Let’s discuss them one by one.

Check if a vector is a subset of another vector using STL Algo includes()

Sort vectors and then pass the start and end iterators of both the sorted vectors, to the std::include() function. It will return true if all elements of a second vector exists in first vector. Let’s see complete example,

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

/*
Check if first vector contains all elements of second vector at any position.
or check if second vector is a subset of first vector irrespecive of element positions.
*/
template <typename T>
bool contains(std::vector<T> first, std::vector<T> second)
{
    // Sort first vector
    std::sort(first.begin(), first.end());
    // Sort second vector
    std::sort(second.begin(), second.end());

    // Check if  all elements of a second vector exists in first vector
    return std::includes(first.begin(), first.end(), second.begin(), second.end());
}

int main()
{
    std::vector<int> firstVec = { 11, 12, 13, 17, 19, 10};
    std::vector<int> secondVec = { 19, 13, 12};

    // Check if vector secondVec is a subset of vector firstVec
    if (contains(firstVec, secondVec))
    {
        std::cout<<"First Vector contains all elements of second vector" <<std::endl;
    }
    else
    {
        std::cout<<"Second vector is not a subset of first vector" <<std::endl;
    }

    return 0;
}

Output:

First Vector contains all elements of second vector

It proved that the second vector is a subset of the first vector.

Check if a vector is a subset of another vector without sorting

If you don’t want to sort the elements of vector, then you can use this logic. Here we are using a for loop and std::find() function. Basically, we will iterate over all elements of second vector, and for each element we will check if it exists in the first vector or not. Let’s see complete example,

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

/*
Check if first vector contains all elements of second vector at any position.
or check if second vector is a subset of first vector irrespecive of element positions.
*/
template <typename T>
bool contains(std::vector<T> first, std::vector<T> second)
{
    bool result = true;
    // Iterate over all elements of second vector
    for(auto const& elem: second)
    {
        // For each element of second vector, check if it exists in the first vector
        if(std::find(first.begin(), first.end(), elem) == first.end())
        {
            result = false;
            break;
        }
    }
   return result;
}

int main()
{
    std::vector<int> firstVec = { 11, 12, 13, 17, 19, 10};
    std::vector<int> secondVec = { 19, 13, 12};

    // Check if vector secondVec is a subset of vector firstVec
    if (contains(firstVec, secondVec))
    {
        std::cout<<"First Vector contains all elements of second vector" <<std::endl;
    }
    else
    {
        std::cout<<"Second vector is not a subset of first vector" <<std::endl;
    }

    return 0;
}

Output:

First Vector contains all elements of second vector

It proved that the second vector is a subset of the first vector.

Summary

We learned, how to check if a vector contains all the elements of another vector in C++.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.