To check if a vector contains all the items of another vector in C++, you can use the std::all_of
algorithm combined with the std::find
algorithm.
In this example,
#include <vector>
, #include <algorithm>
, and #include <iostream>
for working with vectors, algorithms, and input/output streams.vec1
with a set of numerical values.vec2
with a different set of numerical values.std::all_of
algorithm to check if all elements of vec2
are present in vec1
. The std::all_of
algorithm takes a range and a predicate. The range is defined by vec2.begin()
and vec2.end()
, and the predicate is a lambda function that checks if each element of vec2
is found in vec1
using the std::find
algorithm.std::find
algorithm searches for an element in vec1
and returns an iterator to the found element or vec1.end()
if the element is not found.std::all_of
algorithm returns true
if the predicate is true for all elements in the range; otherwise, it returns false
.std::cout
.#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5};
std::vector<int> vec2 = {2, 3, 4};
bool result = std::all_of(vec2.begin(), vec2.end(), [&vec1](int item) {
return std::find(vec1.begin(), vec1.end(), item) != vec1.end();
});
std::cout << std::boolalpha << result << std::endl;
return 0;
}
true
In this example,
#include <vector>
, #include <algorithm>
, and #include <iostream>
for working with vectors, algorithms, and input/output streams.main_vec
with a series of character strings.sub_vec
with a subset of character strings.std::all_of
algorithm to check if all elements of sub_vec
are present in main_vec
. The std::all_of
algorithm takes a range and a predicate. The range is defined by sub_vec.begin()
and sub_vec.end()
, and the predicate is a lambda function that checks if each element of sub_vec
is found in main_vec
using the std::find
algorithm.std::find
algorithm searches for an element in main_vec
and returns an iterator to the found element or main_vec.end()
if the element is not found.std::all_of
algorithm returns true
if the predicate is true for all elements in the range; otherwise, it returns false
.std::cout
.#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<std::string> main_vec = {"apple", "banana", "cherry", "date", "fig"};
std::vector<std::string> sub_vec = {"banana", "cherry", "date"};
bool result = std::all_of(sub_vec.begin(), sub_vec.end(), [&main_vec](const std::string& item) {
return std::find(main_vec.begin(), main_vec.end(), item) != main_vec.end();
});
std::cout << std::boolalpha << result << std::endl;
return 0;
}
true
In this example,
#include <vector>
, #include <algorithm>
, and #include <iostream>
for working with vectors, algorithms, and input/output streams.primary_vec
with a list of mixed data types including numbers and strings.secondary_vec
with some elements that are not present in primary_vec
.std::all_of
algorithm to check if all elements of secondary_vec
are present in primary_vec
. The std::all_of
algorithm takes a range and a predicate. The range is defined by secondary_vec.begin()
and secondary_vec.end()
, and the predicate is a lambda function that checks if each element of secondary_vec
is found in primary_vec
using the std::find
algorithm.std::find
algorithm searches for an element in primary_vec
and returns an iterator to the found element or primary_vec.end()
if the element is not found.secondary_vec
are in primary_vec
, the std::all_of
algorithm returns false
.std::cout
.#include <vector>
#include <algorithm>
#include <iostream>
int main() {
std::vector<std::variant<int, std::string>> primary_vec = {10, 20, 30, "apple", "banana"};
std::vector<std::variant<int, std::string>> secondary_vec = {20, "banana", "cherry"};
bool result = std::all_of(secondary_vec.begin(), secondary_vec.end(), [&primary_vec](const auto& item) {
return std::find(primary_vec.begin(), primary_vec.end(), item) != primary_vec.end();
});
std::cout << std::boolalpha << result << std::endl;
return 0;
}
false
In this tutorial, we learned How to check if a Vector contains all the items of Another Vector in C++ language with well detailed examples.