74.4k views
5 votes
gWrite a function with two input parameters that are both vector of ints. The function returns true if the 2nd vector contains at least one of each value within the 1st vector, otherwise returns false. For example, if the 1st vector contains the values [6 3 8 3] and the 2nd vector contains the values [3 8 8 1 6], the function should return true since the values 6, 3, and 8 all show up at least once in the 2nd vector. However, if the 2nd vector contains only [3 8 8 1], the function should return false since the value 6 does not show up in the 2nd vector.

User Derek K
by
3.5k points

1 Answer

1 vote

Solution and Explanation:

#include <iostream>

#include <vector>

using namespace std;

bool contains_all(vector<int> v1, vector<int> v2) {

for (int i = 0; i < v1.size(); ++i) {

bool found = false;

for (int j = 0; j < v2.size(); ++j) {

if (v1[i] == v2[j])

found = true;

}

if (!found)

return false;

}

return true;

}

int main() {

vector<int> v1 = {6, 3, 8, 3}, v2 = {3, 8, 8, 1, 6}, v3 = {3, 8, 8, 1};

cout << contains_all(v1, v2) << endl;

cout << contains_all(v1, v3) << endl;

return 0;

}

User Donetta
by
3.3k points