fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <regex>
  5. #include <algorithm>
  6.  
  7. int main() {
  8. std::vector<std::string> Arr = {"1.1", "2.4.5", "1.1.1", "3.4"};
  9. std::map<std::string, std::string> Tmp;
  10.  
  11. std::regex re(R"((.+?)\.\d)");
  12. std::smatch match;
  13.  
  14. for (const auto& i : Arr) {
  15. Tmp[i] = "*";
  16. std::string j = i;
  17. while (std::regex_search(j, match, re)) {
  18. j = match.str(1);
  19. Tmp[j] = "*";
  20. }
  21. }
  22.  
  23. std::vector<std::string> Res;
  24. for (const auto& pair : Tmp) {
  25. Res.push_back(pair.first);
  26. }
  27.  
  28. std::sort(Res.begin(), Res.end(), [](const std::string& a, const std::string& b) {
  29. return (a.length() == b.length()) ? a < b : a.length() > b.length();
  30. });
  31.  
  32. for (const auto& item : Res) {
  33. std::cout << item << std::endl;
  34. }
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
1.1.1
2.4.5
1.1
3.4
1
2
3