Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. #include <vector>
  4. #include <map>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> split(const string& value, const string& separator)
  10. {
  11. vector<string> res;
  12. string::size_type pos_start = 0;
  13. string::size_type pos_end = value.find_first_of(separator);
  14. while (string::npos != pos_end)
  15. {
  16. if (pos_end - pos_start > 0)
  17. res.push_back(value.substr(pos_start, pos_end - pos_start));
  18. pos_start = pos_end + 1;
  19. pos_end = value.find_first_of(separator, pos_start);
  20. }
  21. if (value.length() > pos_start)
  22. res.push_back(value.substr(pos_start));
  23. return res;
  24. }
  25.  
  26. string join(vector<string>& value, const string& separator)
  27. {
  28. string res;
  29. for (vector<string>::iterator it = value.begin(); it != value.end(); it++)
  30. {
  31. res += (*it) + separator;
  32. }
  33. return res;
  34. }
  35.  
  36. int main()
  37. {
  38. ifstream ifs("c:/data/desktop/data.txt");
  39. string line;
  40. map<string, vector<string> > res;
  41. while (getline(ifs, line))
  42. {
  43. vector<string> digit_and_letters = split(line, ":");
  44. vector<string> letters = split(digit_and_letters[1], " ");
  45. for (vector<string>::iterator it = letters.begin(); it != letters.end(); it++)
  46. {
  47. res[*it].push_back(digit_and_letters[0]);
  48. }
  49. }
  50.  
  51. for (map<string, vector<string> >::iterator it = res.begin(); it != res.end(); it++)
  52. {
  53. cout << (*it).first << ": " << join((*it).second, " ") << endl;
  54. }
  55.  
  56. return 0;
  57. }
Add Comment
Please, Sign In to add comment