fork download
  1. #include <algorithm>
  2. #include <string>
  3. #include <vector>
  4. #include <cctype>
  5. #include <iostream>
  6.  
  7. typedef std::vector<std::string> StringVect;
  8. std::string sequence = "QWERTYUIOPASDFGHJKLZXCVBNM";
  9. std::vector<int> lookup(26);
  10.  
  11. bool getless(const std::string& s1, const std::string& s2)
  12. {
  13. for (size_t i = 0; i < std::min(s1.size(), s2.size()); ++i)
  14. if (s1[i] != s2[i])
  15. return (lookup[toupper(s1[i])-'A'] < lookup[toupper(s2[i])-'A']);
  16. return s1.size() < s2.size();
  17. };
  18.  
  19. int main()
  20. {
  21. StringVect sv = {{ "apple" },{ "pear" },{ "peach" }};
  22.  
  23. // build the lookup table
  24. int i = 0;
  25. std::for_each(sequence.begin(), sequence.end(), [&](char ch) {lookup[ch-'A'] = i; ++i; });
  26.  
  27. // sort the data
  28. std::sort(sv.begin(), sv.end(), getless);
  29.  
  30. // output results
  31. for (auto& s : sv)
  32. std::cout << s << "\n";
  33. }
Success #stdin #stdout 0s 15248KB
stdin
Standard input is empty
stdout
pear
peach
apple