Set Algorithms

Set Intersection with Different Containers

Can set_intersection() be used with different types of containers?

Yes, std::ranges::set_intersection() can be used with different types of containers as long as they support forward iterators and the elements can be compared using the default comparator or a provided customÂ comparator.

The algorithm requires that the elements are sorted within each container, but the types of the containers themselves do not need to be theÂ same.

Hereâ€™s an example using a std::vector and a std::set:

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

int main() {
std::vector<int> A {1, 2, 3, 4};
std::set<int> B {3, 4, 5, 6};
std::vector<int> Results;
Results.resize(std::min(A.size(), B.size()));

std::ranges::set_intersection(A, B, Results.begin());

for (auto x : Results) {
std::cout << x << ", ";
}
}
3, 4, 0, 0,

In this example, A is a std::vector and B is a std::set. The result shows the intersection of the two sets. The trailing zeroes indicate unused elements in the result vector, which can beÂ removed:

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

int main() {
std::vector<int> A{1, 2, 3, 4};
std::set<int> B{3, 4, 5, 6};
std::vector<int> Results;
Results.resize(std::min(A.size(), B.size()));

auto [AEnd, BEnd, IntersectionEnd] =
std::ranges::set_intersection(
A, B, Results.begin());

Results.erase(IntersectionEnd, Results.end());

for (auto x : Results) {
std::cout << x << ", ";
}
}
3, 4,

Using Different Container Types

When using different container types, ensure they are sorted and compatible with the same comparator. Hereâ€™s an example using a std::list and a std::vector:

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

int main() {
std::list<int> A{1, 2, 3, 4};
std::vector<int> B{3, 4, 5, 6};
std::vector<int> Results;
Results.resize(std::min(A.size(), B.size()));

std::ranges::set_intersection(
A, B, Results.begin());

for (auto x : Results) {
std::cout << x << ", ";
}
}
3, 4, 0, 0,

By using different containers like std::list and std::vector, the intersection algorithm works as long as the inputs are sorted and compatible with the required iteratorÂ types.

Always ensure that your input containers are forward iterable and sorted to use std::ranges::set_intersection()Â effectively.

This Question is from the Lesson:

Set Algorithms

An introduction to set algorithms, and how to implement them using the C++ standard library

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Set Algorithms

An introduction to set algorithms, and how to implement them using the C++ standard library

Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.