fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. // create vector holding desired ordering
  9. vector<string> order({ "a", "d", "i", "n", "ns", "ne", "vl", "rr" });
  10.  
  11. // create sort function for map
  12. struct my_comparator {
  13. bool operator()(const string &s1, const string &s2) {
  14. // find() - it.begin() gives you the index number.
  15. int a = find(order.begin(), order.end(), s1) - order.begin();
  16. int b = find(order.begin(), order.end(), s2) - order.begin();
  17. return a < b; // lower index < higher index
  18. }
  19. };
  20.  
  21. int main() {
  22. // create the map
  23. map<string,string, my_comparator> MyMap = { { "a","legato" }, { "i","3" }, { "vl","3" }, { "rr","2" } };
  24.  
  25. // output map
  26. for (auto& it : MyMap) cout << it.first << "=" << it.second << " ";
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
a=legato  i=3  vl=3  rr=2