fork(3) download
  1. #include <iostream>
  2. #include <regex>
  3.  
  4. using std::cout;
  5. using std::endl;
  6. using std::pair;
  7. using std::regex;
  8. using std::vector;
  9. using std::string;
  10. using std::smatch;
  11. using std::regex_match;
  12.  
  13. typedef vector<pair<string,int>> ResType;
  14.  
  15. void WordsCount(string T, ResType &Res) {
  16. smatch Matches;
  17. regex RegexpDot("^.*?(.+?)\\.\\s*(.*)$");
  18. regex RegexpWord("^.*?([a-zA-Z]+)\\s*(.*)$");
  19. vector<string> Lines;
  20. while(regex_match(T, Matches, RegexpDot)) { Lines.push_back(Matches[1]); T=Matches[2]; }
  21. for(const auto &i:Lines) {
  22. int N = 0;
  23. string Tmp = i;
  24. while(regex_match(Tmp, Matches, RegexpWord)) { Tmp=Matches[2]; N++; }
  25. Res.push_back({i,N});
  26. }
  27. }
  28.  
  29. int main() {
  30. try {
  31. ResType Res;
  32. string Text = "mama mila ramu. Rama vusmert' zadolbala mamu. 128 raz.";
  33. WordsCount(Text,Res);
  34. for(const auto &i:Res) cout << "\"" << i.first << "\": " << i.second << endl;
  35. } catch (std::regex_error& Err) {
  36. std::cout << "Засада: " << Err.what();
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 3556KB
stdin
Standard input is empty
stdout
"mama mila ramu": 3
"Rama vusmert' zadolbala mamu": 4
"128 raz": 1