MultiMap comparison object with key and value pair - c++

I want to write a comparison object for my multimap which is sorted according to keys and if two keys are equal then according to their value.
Problem I am facing is that that inbuilt library of multimap passes only key in comparison object and not value.
Please, can someone help me how can I do this..

Related

How does std::unordered_map find values?

When iterating over an unordered map of values, std::unordered_map<Foo, Bar>, it will use an iterator pointing to values instd::pair<Foo, Bar>. This makes it seem like std::unordered_map stores its values internally as std::pairs of values.
If I'm not mistaken, std::unordered_map works by hashing the key and using that for lookups. So, if the internal structure is something like a list of pairs, how does it know which pair has the hashed key? Wouldn't it need to hash the whole pair, value included?
If the internal structure does not hold pairs, does calling std::unordered_map::begin() then create a std::pair object using the data in the map? Then how does modifying the data in the pair also modify the data in the actual map itself?
Thank you.
Let's pretend that the map is really a single vector:
std::vector<std::pair<Foo, Bar>> container;
It's easy to visualize that iterating over this container iterates over std::pairs of two classes.
The real unordered map works the same way, except instead of a vector the map stores std::pair<Foo, Bar>s in a hash table, with additional pointers that stitch the whole hash table together, that are not exposed via the map's iterators.
The way that associative containers, like maps, get loosely explained and described -- as a key/value lookup -- makes it sound like in the maps keys and values are stored separately: here's a hash table of keys, and each key points to its corresponding value. However that is not the case. The key and its value are kept together, tightly coupled in discrete std::pair objects, and the map's internal structure arranges them in a hashed table that the iterator knows how to iterate over.
So, if the internal structure is something like a list of pairs, how does it know which pair has the hashed key?
Neither. An unordered map can be loosely described as a:
std::vector<std::list<std::pair<Key, Value>>>
A key's hash is the index in the vector. All Keys in the ith position in this vector have the same hash value. All keys with the same hash are stored in a single linked list.

C++ Access key value pairs in map without iterators

I need to check if two std::maps contain the same keys and values so I need a way to access the key value pairs of both std::maps. I know that std::maps have the == operator but my std::map contains objects in the value field that don't have a defined == operator. It would also be too difficult to make an == operator for the object because it contains pointers to other objects I would need to define == for.
Is there a way of getting key value pairs without iterators or a better way of trying to do what I'm describing?

C++- does map sort by key, and then value?

I understand that the map type in C++ automatically sorts its entries by the key. However, how does it handle breaking ties? Does it then sort according to value?
The comparator for C++ map and multimap only sees the keys, not the values. There is no need to "break ties": two keys are considered equal if and only if neither one compares less than the other.

Difference between multimap and unordered map

I was reading about c++ multimap and it states that multimap allows inserting duplicate Keys.This makes me think that a multimap uses a hashmap for underneath implementation (since a hashmap allows duplicates incase of collision) then I also read that unordered map uses a hash map. My question is then whats the difference between the two. I tried looking for comparison between the two but I could not find anything that would explain this.
A hash table permits and resolves collisions of hash values. It only stores one copy of each key value (as determined by operator== or another equality relation). But if two distinct keys hash to the same value (bound to happen), that is handled by collision resolution. For each key, there is a single value.
A multimap is an ADT that associates multiple values with each key instead of one. It's not a particular implementation strategy: It can internally use a hash table, a search tree, or something completely different.

what C++ map mean?

I have the following C++ map example code:
map<string,string> &weight2price
....
weight = ...
price = ...
if(weight2price.find(weight) == weight2price.end())
weight2price[weight] = price;
could anyone tell me what it means by
if(weight2price.find(weight) == weight2price.end())
The STL Map is a standard C++ container. It uses unique keys that can be compared to refer to value data being stored inside the container. It works much like an array but unlike an array, the key can be anything and doesn't need to be an integer.
The syntax for a map is:
std::map < Key, Data, Compare, Alloc>
Key - the type of object that will be used as key
Data - the type of object that will be used as data
Compare - a comparison function for the keys
Alloc - The map's allocator, used for all internal memory management
usage:
map[Key] = Data
For further reading about STL maps look here :
http://en.wikipedia.org/wiki/Map_%28C%2B%2B%29
and here: http://www.sgi.com/tech/stl/Map.html
In the code snippet, you are using the function find() from the map. This function returns an iterator pointing to the element from the map that contains the key you were searching for. If that key is not found, the find function will return the iterator pointing to the end of the map. That is what is being checked in the code snippet you attached to your question and if the find function returned the value of the end iterator, this means that the key is not in the map, so it adds the key and data to the map. An optimization for your code was provided by GMan in his comment.
A very basic explanation for an iterator (although not complete) would be that an iterator is a pointer to a pair< Key,Data>.
Hope this helps!
If the key passed to find() does not exist, find() returns end(). So it's saying "If it wasn't found...".
The code could be written better as:
weight2price.insert(std::make_pair(weight, price));
So look-up isn't done twice. (If the key already exists, insert won't do anything.)
It means that weight was not found. Then if it wasn't found, it adds it.
map in C++ is a like hashmap, where a key (in your case weight) is mapped to a value (price).
Here it checks to see if the particular weight is already mapped in this map, if not the find method will return weight2price.end()
In that case the program enters a key-value pair in the map with the line that you greyed out.