Intersection of two std unordered map

0 votes

I have two std::unordered_map

std::unordered_map<int, int> mp1;
std::unordered_map<int, int> mp2;

I need to find the intersection of key-value pairs and save it in another map of the same type.

std::unordered_map<int, int> mp;

How can i do this??

May 31, 2022 in C++ by Nicholas
• 7,760 points
1,016 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You could fill a new container with the key, value pairs that exist in both maps using std::set intersection. 

Set intersection requires sorted ranges (which an unordered map will not provide), so either replace the unordered maps with map or create temporary maps (or temporary std::setstd::pair<int, int>>s) before using set intersection.

If you use intersections frequently, I propose replacing your initial unordered maps with ordered maps for efficiency:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <unordered_map>
#include <vector>

int main() {
    std::map<int, int> mp1 {{1,0}, {2,0}, {3,0}};
    std::map<int, int> mp2 {{0,0}, {2,0}, {3,0}};

    // this can be unordered unless you plan to use it in an intersection later:
    std::unordered_map<int, int> mp;

    std::set_intersection(
        mp1.begin(), mp1.end(),
        mp2.begin(), mp2.end(), 
        std::inserter(mp, mp.begin())
    );

    for(auto[key, val] : mp) {
        std::cout << key << ',' << val << '\n';
    }
}
answered Jun 1, 2022 by Damon
• 4,960 points

edited Mar 5

Related Questions In C++

0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
1,364 views
0 votes
1 answer

Initializing a two dimensional std::vector

Assume you wish to start a 2D vector, m*n, with a value of 0. We could do it. #include<iostream> int main(){ int ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
598 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
2,232 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
749 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
962 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
3,174 views
0 votes
0 answers

How to choose between map and unordered_map?

Assume I wanted to map data that ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
627 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
794 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,545 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,562 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP