Set Algorithms

Set Intersection with Different Containers

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

Abstract art representing computer programming

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.

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

A computer programmer
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.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved