Last key in a std::map

Yes. Map is a sorted container, the reverse iterator must return the elements in reverse (i.e. decreasing) order of their keys. [Edit: as Charles Bailey points out in his answer, your code gives the greatest key if it exists – i.e. if the map is non-empty]

What is difference between const and non const key?

int and const int are two distinct types. std::map<int, float> and std::map<const int, float> are, similarly, different types. The difference between std::map<const int, float> and std::map<int, float> is, to a degree, analogous to the difference between, say, std::map<int, float> and std::map<std::string, float>; you get a fresh map type for each. In the non-const case, the … Read more

How can I get a value from a map?

std::map::operator[] is a non-const member function, and you have a const reference. You either need to change the signature of function or do: MAP::const_iterator pos = map.find(“string”); if (pos == map.end()) { //handle the error } else { std::string value = pos->second; … } operator[] handles the error by adding a default-constructed value to the … Read more

How can I use std::maps with user-defined types as key?

You don’t have to define operator< for your class, actually. You can also make a comparator function object class for it, and use that to specialize std::map. To extend your example: struct Class1Compare { bool operator() (const Class1& lhs, const Class1& rhs) const { return lhs.id < rhs.id; } }; std::map<Class1, int, Class1Compare> c2int; It … Read more

How can I merge two STL maps?

Assuming you want to preserve the elements in mapA, and merge elements in mapB for which there is no key in mapA: mapA.insert(mapB.begin(), mapB.end()) will do what you want, I think. (EDIT: If you are using C++17 or newer, consider this answer: https://stackoverflow.com/a/56594603/118150) Working example: #include <iostream> #include <map> void printIt(std::map<int,int> m) { for(std::map<int,int>::iterator it=m.begin();it!=m.end();++it) … Read more

Using char* as a key in std::map

You need to give a comparison functor to the map otherwise it’s comparing the pointer, not the null-terminated string it points to. In general, this is the case anytime you want your map key to be a pointer. For example: struct cmp_str { bool operator()(char const *a, char const *b) const { return std::strcmp(a, b) … Read more

How to update std::map after using the find method?

std::map::find returns an iterator to the found element (or to the end() if the element was not found). So long as the map is not const, you can modify the element pointed to by the iterator: std::map<char, int> m; m.insert(std::make_pair(‘c’, 0)); // c is for cookie std::map<char, int>::iterator it = m.find(‘c’); if (it != m.end()) … Read more

How can I create my own comparator for a map?

std::map takes up to four template type arguments, the third one being a comparator. E.g.: struct cmpByStringLength { bool operator()(const std::string& a, const std::string& b) const { return a.length() < b.length(); } }; // … std::map<std::string, std::string, cmpByStringLength> myMap; Alternatively you could also pass a comparator to maps constructor. Note however that when comparing by … Read more