forked from keineahnung2345/cpp-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincludes(subset).cpp
38 lines (29 loc) · 1.12 KB
/
includes(subset).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//https://stackoverflow.com/questions/6906521/how-to-check-whether-a-vector-is-a-subset-of-another-in-c
//https://en.cppreference.com/w/cpp/algorithm/includes
template <typename T>
bool IsSubset(std::vector<T> A, std::vector<T> B){
std::sort(A.begin(), A.end());
std::sort(B.begin(), B.end());
return std::includes(A.begin(), A.end(), B.begin(), B.end());
}
int main() {
vector<vector<string>> favoriteCompanies = {
{"leetcode","google","facebook"},
{"google","microsoft"},
{"google","facebook"},
{"google"},
{"amazon"}
};
bool included = std::includes(favoriteCompanies[0].begin(), favoriteCompanies[0].end(),
favoriteCompanies[2].begin(), favoriteCompanies[2].end());
cout << "std::includes?: " << included << endl;
//before using std::includes, we need to sort the vectors!!
included = IsSubset(favoriteCompanies[0], favoriteCompanies[2]);
cout << "sort and then std::includes?: " << included << endl;
return 0;
}