fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <utility>
  5.  
  6. using Match = std::pair<std::string::size_type, std::string>;
  7. using MatchVec = std::vector<Match>;
  8.  
  9. MatchVec findPattern(const std::string &str, const std::string &pattern)
  10. {
  11. MatchVec vec;
  12. Match m;
  13.  
  14. std::string::size_type index, pos = 0;
  15.  
  16. while ((index = str.find(pattern, pos)) != std::string::npos) {
  17. m.first = index;
  18. index += pattern.length();
  19. m.second = str.substr(index, 6);
  20. vec.push_back(m);
  21. pos = index;
  22. }
  23.  
  24. return vec;
  25. }
  26.  
  27. int main()
  28. {
  29. std::string str1 = "abccccfffcccccc";
  30. std::string str2 = "ab";
  31.  
  32. auto matches = findPattern(str1, str2);
  33. for(auto &match : matches) {
  34. std::cout << "Match found at position: " << match.first << std::endl;
  35. std::cout << "String is " << match.second << std::endl;
  36. }
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 4472KB
stdin
Standard input is empty
stdout
Match found at position: 0
String is ccccff