Advertisement
Guest User

Untitled

a guest
May 24th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <fstream>
  4. #include <vector>
  5. #include <iterator>
  6. #include <string>
  7. #include <sstream>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. class Record {
  13. public:
  14. int score;
  15. string name;
  16.  
  17. Record(int score, const string &name) : score(score), name(name) {}
  18.  
  19. };
  20.  
  21. using namespace std;
  22. int main() {
  23. vector<Record*> test;
  24. ifstream in("test.txt");
  25. string teststr;
  26. while (!in.eof()) {
  27. getline(in, teststr);
  28. std::size_t pos = teststr.find(":");
  29. string s2 = teststr.substr(pos+1, 13);
  30. string s3 = teststr.substr(0, pos);
  31. std::istringstream ist(s3);
  32. int hey = 0;
  33. ist >> hey;
  34. if (hey == 0) {
  35. break;
  36. }
  37. else {
  38. test.push_back(new Record(hey, s2));
  39. }
  40. }
  41. test.push_back(new Record(25000, "A")); // Добавление значений
  42. test.push_back(new Record(15000, "B"));
  43. test.push_back(new Record(45000, "C"));
  44. test.push_back(new Record(44999, "Hai"));
  45. //sort(test.rbegin(), test.rend());
  46.  
  47.  
  48.  
  49. for (int i = 0; i < test.size()-1; i++) {
  50. for (int j = 0; j < test.size() - i - 1; j++) {
  51. if (test[j]->score < test[j+1]->score) {
  52. int temp = test[j]->score;
  53. test[j]->score = test[j+1]->score; // Отсортировали вектор пузырьком
  54. test[j+1]->score = temp;
  55. string templ = test[j]->name;
  56. test[j]->name = test[j+1]->name;
  57. test[j+1]->name = templ;
  58. }
  59. }
  60. }
  61. ofstream hala("test.txt");
  62.  
  63. for (int i = 0; i < test.size(); i++) {
  64. hala << test[i]->score << ":" << test[i]->name << endl; // Записали его в файл. В качестве разделителя использовал :
  65. }
  66. hala.close();
  67. test.clear();
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement