fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <string>
  5. #include <algorithm>
  6. #include <set>
  7. using namespace std;
  8.  
  9. vector<pair<string,string>> KeySort(map<string,string> s, const vector<string>& order){
  10. vector<pair<string,string>> res;
  11. for (const auto& it : order) {
  12. auto needle = s.find(it);
  13. if(needle != s.end()){
  14. res.emplace_back(move(*needle));
  15. s.erase(needle);
  16. }
  17. }
  18. for (auto&& it : s) res.emplace_back(move(it));
  19. return res;
  20. }
  21.  
  22. int main() {
  23. // create vector holding desired ordering
  24. vector<string> ord({ "a", "d", "i", "n", "ns", "ne", "vl", "rr" });
  25.  
  26. // create the map
  27. map<string,string> MyMap = { {"a","legato"}, {"vl","4"}, {"i","2"}, {"rr","3"}, {"z","unspecified1"}, {"b","unspecified2"}};
  28.  
  29. // sort the vector
  30. vector<pair<string,string>> out = KeySort(MyMap, ord);
  31.  
  32. // put the vector pairs into a string format
  33. for (auto& it : out) cout << it.first << "=" << it.second << " ";
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
a=legato i=2 vl=4 rr=3 b=unspecified2 z=unspecified1