Hi!
What is the canonical way of finding an intersection of two
std::maps?
i.e. I have
std::map<whatev er,size_tmap1;
std::map<whatev er,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. 3 6461 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_inters ection 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<whatev er,size_tmap1;
std::map<whatev er,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.
On Nov 5, 8:30 pm, Mark P <use...@fall200 5REMOVE.fastmai lCAPS.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.
On 6 Lis, 04:09, "Jim Langston" <tazmas...@rock etmail.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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 <iostream.h>
#include <string.h>
#include <map>
|
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){}
|
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 std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >,class CTCSystem *,struct
std::less<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >,class std::allocator<struct...
|
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,
--
| |
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 detail syntax of that...
How to compare a character string with one of the value in that
structure????????
|
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 before, but it's nearly impossible to find.
I'm having trouble using STL vectors, maps, ... in templated functions, when the templated class is a template parameter of the map too.
Below is a (cannibalized) example of a class 'Model' which contains...
|
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, NodePtr>::iterator NodeMapItr;
NodeMap nmap;
In my classes, I often find myself copying the second arguments of
|
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;
..
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |