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.
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 -
struct key_obj_less : less<key_obj>
-
{
-
bool operator()(const key_obj & x, const key_obj & y) const
-
{
-
// Code here to determin with object is less than the other
-
}
-
};
-
then you can declare your multimap as
multimap<key_obj, value_obj, key_obj_less> mmap;
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: -
bool unknown418(key_obj first, key_obj second);
-
Then you create your multimap by supplying the address of this function: -
multimap<key_obj, value_obj, unknown418> mmap;
-
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: -
class key{
-
int x;
-
int y;
-
....
-
bool operator == (key& k){
-
if((k.x == x && k.y==y)|(k.x == y && k.y==x))
-
return true;
-
else
-
return false;
-
-
bool compare (key& k){
-
if((k.x<x && k.y<y)|(k.x <y && k.y<x))
-
return true;
-
else
-
return false;
-
-
bool compare(key* k){
-
if((k->x < x && k->y<y)|(k->x < y && k->y<x))
-
return true;
-
else
-
return false;
-
}
-
-
-
};
-
-
class value{
-
....
-
};
-
-
class top{
-
multimap<key*, value*, compare> mmap1;
-
multimap<key, value*, compare> mmap2;
-
}
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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;
|
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...
|
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,
|
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...
|
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...
|
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...
|
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.
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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...
|
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,...
| |