473,396 Members | 1,826 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,396 software developers and data experts.

Multimap with objects

I am confused with how to implement the multimap if both key and values are different objects. I don't get how to define this comparison function, where it should be and what it should do. If the keys are key_obj, and values are value_obj then you have
multimap<key_obj, value_obj, _______> mmap;

I can't seem to google up any examples.
Nov 2 '07 #1
4 2017
Banfa
9,065 Expert Mod 8TB
You basically have 2 options the first is to put a operator< overload in your key_obj class, if you do this then you can declare you map as

multimap<key_obj, value_obj> mmap;

The second is to subclass from less<class T> specialising for your class, something like

Expand|Select|Wrap|Line Numbers
  1. struct key_obj_less : less<key_obj>
  2. {
  3.     bool operator()(const key_obj & x, const key_obj & y) const
  4.     {
  5.         // Code here to determin with object is less than the other
  6.     }
  7. };
  8.  
then you can declare your multimap as

multimap<key_obj, value_obj, key_obj_less> mmap;
Nov 2 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
multimap<key_obj, value_obj, _______> mmap;
The multimap requires a binary predicate for the comparator function. This function is called to compare two keys so the map can place the key in the right place.

However, you don't need this comparator function if:
1) the key_obj class can implement an operator<().
2) there is an operator<() that has two key_obj arguments.

Otherwise you can write the comparator yourself.

The comparator functon for multimap must be a binary predicate. That is any function you choose provided it returns a bool and has two key_obj arguments:

Expand|Select|Wrap|Line Numbers
  1. bool unknown418(key_obj first, key_obj second);
  2.  
Then you create your multimap by supplying the address of this function:
Expand|Select|Wrap|Line Numbers
  1. multimap<key_obj, value_obj, unknown418> mmap;
  2.  
Nov 2 '07 #3
Thanks for the replys.
I tried writing my own compartor function but it didn't work, it said it wasnt' within scope.

What I want to do is actually have the keys and values be pointers to objects, so that I don't keep creating them. I might end up with > 1000 of these objects.
But even if I use the actual objects for the keys it still doesn't work.
This is what I have currently:
Expand|Select|Wrap|Line Numbers
  1. class key{
  2. int x;
  3. int y;
  4. ....
  5. bool operator == (key& k){
  6.    if((k.x == x &&  k.y==y)|(k.x == y && k.y==x))
  7.       return true;
  8.    else
  9.       return false;
  10.  
  11. bool compare (key& k){
  12.    if((k.x<x &&  k.y<y)|(k.x <y && k.y<x))
  13.       return true;
  14.    else
  15.       return false;
  16.  
  17. bool compare(key* k){
  18.    if((k->x < x &&  k->y<y)|(k->x < y && k->y<x))
  19.       return true;
  20.    else
  21.       return false;
  22. }
  23.  
  24.  
  25. };
  26.  
  27. class value{
  28. ....
  29. };
  30.  
  31. class top{
  32. multimap<key*, value*, compare> mmap1;
  33. multimap<key, value*, compare> mmap2;
  34. }
  35.  
I have also tried moving the compare functions inside the top class but it doesn't help.


When I create the iterator do use include the compare part or not?
In addition, if I want to use the find() function does the multimap calls the == operator for the object. Do I just overwrite this function.
Nov 2 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Did you not read my Post #3????

Your compare function must be a binary predicate.

The compare function I see are not binary predicates.

Also, the compare nedd only be less than. The function is to return true of the first argument is less than the second argument.

Please read my post again.
Nov 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Tanguy Fautré | last post by:
Hello, does std::multimap make any guarantee about the insertion order? for example: int main() { std::multimap<int, int> Map;
9
by: Dennis Jones | last post by:
Hi, Is there a way to iterate through a multimap in such a way as to encounter only the unique keys? In other words, since a multimap allows duplicate keys, I would like to iterate through the...
3
by: He Shiming | last post by:
Hi Folks, Happy holidays! I have a question regarding STL multimap. Basically, the current multimap<int,int> look like this: key=>value 1=>10, 1=>20, 1=>30,
4
by: Nick Keighley | last post by:
Hi, I've checked out various documentation for multimap but can't find anywhere it explicitly stated that insert() invalidates multimap iterators. consider this pseudo code:- int...
4
by: sks | last post by:
I have a question regarding std::multimap/iterators. At the SGI website, it says "Erasing an element from a multimap also does not invalidate any iterators, except, of course, for iterators that...
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
6
by: castironpi | last post by:
Is multimap just a syntax-checked dictionary of mutable sequences? Is def( a ): a= a trick or part of the language? It looks pretty sharp in variable-width.
1
by: ambarish.mitra | last post by:
Hi all, I have a multimap, where key is an int and the value is a class. I can insert into the multimap, but finding it difficult to retrieve the value when keys match. I can do this with...
20
by: puzzlecracker | last post by:
I am using while loop for that but I am sure you can do it quicker and more syntactically clear with copy function. Here is what I do and would like to if someone has a cleaner solution: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...
0
agi2029
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,...

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.