473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

intersection of two std::maps


Hi!

What is the canonical way of finding an intersection of two
std::maps?
i.e. I have

std::map<whatever,size_tmap1;
std::map<whatever,size_tmap2;

.... and I need an std::vector containing all the values that occur
simultaneously in both of the maps, nevermind the keys. In each
of the maps the values do not repeat.

One idea is to copy all values from map1 into a vector, and then
for each value from map2 check if it's present in the vector already,
if not -- then push it into the vector. This, of course, has ugly
complexity, so perhaps a set would do better. Is there a better
way?

I was also thinking of putting the values into a multiset and then
taking out all values with a count of 2, but there must be a simpler
way.

TIA,
- J.

Nov 5 '07 #1
3 6419
ja************@gmail.com wrote:
Hi!

What is the canonical way of finding an intersection of two
std::maps?
I'm not sure there is one. It's a pretty strange request since,
ignoring the keys, the map is not a very useful value container.
Pushing all of the values into a set is one possibility. I don't see
any advantage to your multiset proposal. I think what I would do is:

1. Copy all values from each map to a separate vector.
2. Sort each vector.
3. Use std::set_intersection to obtain the intersection.

If copying values is too costly you can also use vectors of pointers to
the original values in the map, but then you'll need to write your own
comparison functions for steps 2 and 3, and do some additional copying
at the end.

-Mark
i.e. I have

std::map<whatever,size_tmap1;
std::map<whatever,size_tmap2;

... and I need an std::vector containing all the values that occur
simultaneously in both of the maps, nevermind the keys. In each
of the maps the values do not repeat.

One idea is to copy all values from map1 into a vector, and then
for each value from map2 check if it's present in the vector already,
if not -- then push it into the vector. This, of course, has ugly
complexity, so perhaps a set would do better. Is there a better
way?

I was also thinking of putting the values into a multiset and then
taking out all values with a count of 2, but there must be a simpler
way.

TIA,
- J.
Nov 5 '07 #2
On Nov 5, 8:30 pm, Mark P <use...@fall2005REMOVE.fastmailCAPS.fm>
wrote:
You could do that as well. It's fewer lines of code, perhaps, but
conventional wisdom is that unless you need to sort "online", it's
generally more efficient to collect all values and perform a single sort
operation at the end (as I suggest) rather than maintaining a sorted
structure as items are individually added (the set approach). See Scott
Meyers "Effective STL" and do keep in mind the usual caveats about
premature optimization, but I would expect better performance with the
vector.
I see, thank you.
- J.

Nov 5 '07 #3
On 6 Lis, 04:09, "Jim Langston" <tazmas...@rocketmail.comwrote:
>
Also, your values are not guaranteed to be unique in the map since they are
not keyed.
Note however that in the OP I wrote:

JDIn each of the maps the values do not repeat.

I know for sure that in each of the maps there are no duplicate
values.
Copying to std::vector or std::set probably depends on what you
want to do with duplicate values in the same map. Do you want to enforce
uniqueness on each the values in each copied set/vector? If so, use set.
If not, use vector.

Vector should be faster however for copying the data into as it does not
have to build the index.
I guess I'll stick with vector.

thanks,
- J.

Nov 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike - EMAIL IGNORED | last post by:
I am looking for software that would translate xml strings to and from nested std:maps. Any suggestions? Thanks in advance. Mike.
2
by: ash | last post by:
Hi, I'm getting started with STL, and am stuck at creating a map container. I checked one of the texts and found a code in there. To make it simple, i wrote the following: #include...
4
by: Simon Elliott | last post by:
Here's a small example which uses std::maps within std::maps. Note the line flagged // *** copy? #include <iostream> #include <map> #include <string> struct Tfoo { Tfoo():i1_(0),i2_(0){}
1
by: Jim Tarrant | last post by:
Hi, When I run our application under PurifyPlus, I get lots of "UMR: Uninitialized memory read" warnings, e.g. UMR: Uninitialized memory read in std::_Tree_nod<class std::_Tmap_traits<class...
2
by: Gernot Frisch | last post by:
Hi, I know I can give a std::map a sorting function in the c'tor. However, I would need to _change_ the sorting behaviour of an existing instance. How would I do that? Thank you, --
8
by: Anamika | last post by:
Can I have maps with following properties? Key= A character strings... Value=A structure... Members of structure will be two character strings.... If yes how to do that?Can anyone tell me the...
5
by: DrLex | last post by:
This is a really annoying thing to look up in Google because all pages that mention STL maps or vectors will most likely also contain the word "template". So maybe this question has been asked...
5
by: pallav | last post by:
I have a map like this: typedef boost::shared_ptr<NodeNodePtr; typedef std::vector<NodePtrNodeVecPtr; typedef std::map<std::string, NodePtrNodeMap; typedef std::map<std:string,...
6
by: Hicham Mouline | last post by:
Hello, I am attempting to design a class: class C { std::map<double, const Ama; std::map<double, const Bma; ..
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.