Suggest any method to sort a multimap for both key and its values. For example- Input- (5,1), (1,9), (1,1), (5,2), (1,2) And the Output must be- (1,1), (1,2), (1,9), (5,1), (5,2).
相关问题
- Sorting 3 numbers without branching [closed]
- How to toggle on Order in ReactJS
- PHP Recursively File Folder Scan Sorted by Modific
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
The answer is emplace_hint. Pseudo code will look like that:-
Change the key to include both values. Design a comparator which compares the two pairs of values in the correct order.
Having done this you can use multiset instead of a multimap.
If you really want to use multimap then the ordering of values is always the order in which you insert them and that cannot be changed unfortunately i.e in the example given in the question they are stored as (1,9), (1,1), (1,2), (5, 1), (5,2)
If you can relax a bit on the multimap, then you can use a set and store the above pairs in the set and define the ordering you desire on the pair definition. Since, the set stores it's values in sorted order, it will store your pairs also in the ordering you define.
You just need to copy all its elements to a
multiset<pair<int, int>>
:After this,
multiset<pair<int, int>> b
is what you want, i.e.{(1,1), (1,2), (1,9), (5,1), (5,2)}
.