The error no matching function for call to object of type 'const __set_intersection_fn' when using std::ranges::set_intersection with std::views::keys from std::unordered_map indicates a fundamental mismatch in expectations.
The Problem:
std::ranges::set_intersection (like std::set_intersection) requires its input ranges to be sorted.
std::unordered_map does not store its keys in any particular sorted order; their order is determined by their hash values. Therefore, map1 | std::views::keys and map2 | std::views::keys produce unsorted ranges.
Since there's no overload of std::ranges::set_intersection that works on unsorted inputs (it would be inefficient, requiring sorting internally, which defeats the purpose of a direct set intersection algorithm), the compiler complains.
Solution for std::unordered_maps:
For std::unordered_maps, the most efficient and idiomatic way to find common keys is to iterate through one map and check for the existence of its keys in the other map, leveraging the O(1) average-case lookup time of unordered_map.
#include <unordered_map>
#include <vector>
#include <tuple>
#include <algorithm> // For std::min, though not strictly needed here
#include <iterator> // For std::back_inserter if you still wanted ranges with sorting
// Assuming vec3i and vec3f are defined elsewhere, e.g.:
// struct vec3i { int x, y, z; /* ... operators ==, hash specialization ... */ };
// struct vec3f { float x, y, z; /* ... */ };
// For vec3i to work as a key in unordered_map, you need:
// 1. operator== for equality comparison
// 2. A custom hash specialization or a custom hash function object
// Example hash specialization (place in global or std namespace):
/*
namespace std {
template <>
struct hash<vec3i> {
size_t operator()(const vec3i& v) const {
// A simple combined hash (can be more robust)
size_t h1 = hash<int>()(v.x);
size_t h2 = hash<int>()(v.y);
size_t h3 = hash<int>()(v.z);
// Combine hashes (e.g., boost::hash_combine equivalent)
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
}
*/
auto find_common_keys(const std::unordered_map<vec3i, std::tuple<vec3f, int>>& map1,
const std::unordered_map<vec3i, std::tuple<vec3f, int>>& map2) {
std::vector<vec3i> result;
// Iterate through the smaller map to minimize lookups,
// though for keys either map works fine.
const auto& smaller_map = (map1.size() < map2.size()) ? map1 : map2;
const auto& larger_map = (map1.size() < map2.size()) ? map2 : map1;
for (const auto& pair : smaller_map) {
// In C++20, you can use larger_map.contains(pair.first) which is cleaner
if (larger_map.count(pair.first)) { // count returns 0 or 1 for unique keys
result.push_back(pair.first);
}
}
return result;
}
// Example usage (requires vec3i and vec3f definition and hash specialization)
/*
int main() {
std::unordered_map<vec3i, std::tuple<vec3f, int>> m1;
m1[{1,2,3}] = {vec3f{1.f,2.f,3.f}, 10};
m1[{4,5,6}] = {vec3f{4.f,5.f,6.f}, 20};
m1[{7,8,9}] = {vec3f{7.f,8.f,9.f}, 30};
std::unordered_map<vec3i, std::tuple<vec3f, int>> m2;
m2[{4,5,6}] = {vec3f{4.f,5.f,6.f}, 25};
m2[{7,8,9}] = {vec3f{7.f,8.f,9.f}, 35};
m2[{10,11,12}] = {vec3f{10.f,11.f,12.f}, 40};
std::vector<vec3i> common = find_common_keys(m1, m2);
std::cout << "Common keys:\n";
for (const auto& k : common) {
std::cout << "{" << k.x << "," << k.y << "," << k.z << "}\n";
}
return 0;
}
*/
Alternative (if you must use std::ranges::set_intersection and are willing to sort):
If, for some reason, you really wanted to use std::ranges::set_intersection, you would first have to extract the keys into std::vectors and then sort those vectors:
#include <unordered_map>
#include <vector>
#include <tuple>
#include <algorithm> // For std::ranges::sort, std::ranges::set_intersection
#include <ranges> // For std::views::keys
// Assuming vec3i is defined and has operator< for sorting
// struct vec3i { int x, y, z; /* ... operator< ... */ };
auto find_common_keys_with_sorting(const std::unordered_map<vec3i, std::tuple<vec3f, int>>& map1,
const std::unordered_map<vec3i, std::tuple<vec3f, int>>& map2) {
std::vector<vec3i> keys1;
std::vector<vec3i> keys2;
// Extract keys into vectors
for (const auto& pair : map1) {
keys1.push_back(pair.first);
}
for (const auto& pair : map2) {
keys2.push_back(pair.first);
}
// Sort the key vectors
std::ranges::sort(keys1);
std::ranges::sort(keys2);
std::vector<vec3i> result;
std::ranges::set_intersection(
keys1,
keys2,
std::back_inserter(result));
return result;
}
Important Note for vec3i:
For vec3i to work correctly as a key in std::unordered_map, you need to:
- Define
operator== for vec3i.
- Provide a hash function for
vec3i. This is typically done by specializing std::hash<vec3i> or providing a custom hash function object to the unordered_map constructor.
- For the sorting approach (
find_common_keys_with_sorting), vec3i also needs an operator< defined.
The first solution (iterating and checking count) is generally preferred for unordered_maps as it leverages their O(1) average-case lookup time, avoiding the O(N log N) cost of sorting.