ty1
ty1

Reputation: 364

C++ How to check if contents of vector exist in another vector?

I'm trying to write a program that checks to see if the contents of one vector exist in another. For example:

vector<int> a = {1, 2};
vector<int> b = {6, 5, 3, 1, 9, 2};

This would return true when comparing these two vectors because the contents of a exist somewhere in b.

vector<int> a = {1, 2}
vector<int> b = {3, 1, 5, 6}

This would return false because not everything in a exists in b.

I've tried it using a while loop, but I got stumped on how to get the loop to break.

bool check_vec(vector<int> a, vector<int> b){

    int checker = 0;

    int i = 0;
    int q = 0;

    while ( true ) {
        if(b.at(i) == a.at(q)) {
            checker++;
            i = 0;
            q++;
            if(checker == a.size())
                return true;

            i++;

        }
    }
}

Upvotes: 0

Views: 9167

Answers (3)

This is a more elaborate and analogous approach for someone who has not gone deep into the language as to be aware of those inbuilt functions in the answers above

bool contains(const std::vector<int>& v1,
const std::vector<int>& v2) {
int counter{};
for (size_t i = 0; i < v2.size(); i++)
{
    for (size_t j = 0; j < v1.size(); j++)
    {
        if (v2[i] == v1[j])
        {
            counter++;
            break;
        }
    }
}
if (counter == v2.size())
{
    return true;
}
else return false;

}

Upvotes: 0

Francis Cugler
Francis Cugler

Reputation: 7905

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

int main() {

    std::vector<int> a = {1, 2};
    std::vector<int> b = {6, 5, 3, 1, 9, 2};

    vector<int> c( a.size() + b.size() );
    std::cout "\nFirst vector values are ...\n";
    std::copy( a.begin(), a.end() std::ostream_iterator<int>( std::cout, "\t" );
    std::cout << '\n';

    std::cout << "\nSecond vector values are ...\n" << endl;
    std::copy( b.begin(), b.end(), std::ostream_iterator<int>( std::cout, "\t" );
    std::cout << '\n';

    auto pos = std::set_difference( a.begin(), a.end(), b.begin(), b.end(), c.begin() );
    c.resize( pos - c.begin() );

    std::cout << "\nValues present in vector one but not in vector two are ...\n";
    std::copy( c.begin(), c.end(), std::ostream_iterator<int>( cout, "\t" ) );
    std::cout << '\n';

    c.clear();
    c.resize( a.size() + b.size() );

    pos = std::set_union( a.begin(), a.end(), b.begin(), b.end(), c.begin() );
    v.resize( pos - c.begin() );

    std::cout << "\nMerged vector values in vector are ...\n";
    std::copy( c.begin(), c.end(), std::ostream_iterator<int>( cout , "\t" ) );
    std::cout << "\n\n";

    std::cout << "\nPress any key and enter to quit.\n";
    std::cin.get();

    return 0;
}      

Upvotes: 0

Barmar
Barmar

Reputation: 780842

Use a loop that iterates over the contents of the first vector. You don't need a loop for the second vector, just use std::find.

for (auto a_elt: a) {
    if (std::find(b.begin(), b.end(), a_elt) == b.end()) {
        return false;
    }
}
return true;

You can also use std::all_of:

return std::all_of(a.begin(), a.end(), [](int a_elt) {
    return std::find(b.begin(), b.end(), a_elt) != b.end();
});

Upvotes: 8

Related Questions