You cannot do that. Multimap in C++ STL is ordered and the order cannot/must not be changed (I think at the bottom line it is using a balanced binary tree for the keys I think, not sure though).
What you can do is instantiate a new multimap
object passing a comparator that fits your needs (research strict weak order).
By default, less
is used, which compares to an ascending order. In the example below you can see greater
comparator object passed to the template parameter for the comparator, which compares to descending order.
The comparator compares only keys, not values.
You can pass your own comparator if you want or define your own <
operator for the type (key) you want to store inside the map.
See example below for usage.
// multimap
#include <map>
#include <iostream>
#include <functional>
#include <iomanip>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << start->first << ":" << start->second << " ";
start++;
}
}
template<class T, class U> void fillMap(multimap<T, T> & m, U start, U end) {
for (; start != end; ++start) {
m.insert(pair<T, T>(*start, *start));
}
}
int main() {
int t[] = {2, 10, 8, 4, 5, 5, 3, 10, 7, 2};
//1. standard initialization - default constructor
multimap<int, int > m1;
fillMap(m1, t, t + 10);
//2. iterator constructor
multimap<int, int> m2(m1.begin(), m1.end());
//2b. iterator constructor - wrong
//multimap<int, int> m2(t, t+10);
print(m2.begin(), m2.end());
cout << endl;
//3. copy constructor
multimap<int, int> m3(m2);
print(m3.begin(), m3.end());
cout << endl;
//4. providing different comparator
multimap<int, int, greater<int> > m4(m1.begin(), m1.end());
print(m4.begin(), m4.end());
cout << endl;
//5. Not allowed - source and target object are not of the same type
//multimap<int, greater<int> > s5 (s3);
//6. assignment
multimap<int, int> m6;
m6 = m3;
print(m6.begin(), m6.end());
cout << endl;
//7. Not allowed - assignment of incompatible multimap objects
//m6 = m4;
return 0;
}