Given a file with the results of the game with information about the name and time of the game like this:

6/Maksim 1/Andrey 10/Dmitriy 15/Alexey 8/Liza 

That is, I have a game in which the main goal is to hold out as long as possible. Who lasted longer - that is supposedly the best player. After each game session, request the player's name, and this information is recorded in the Top.txt file in the following format:

 количСство_сСкунд/имя_ΠΈΠ³Ρ€ΠΎΠΊΠ° 

It is necessary to display the sorted results in the console. I honestly have no idea how to do this.
Based on the data above, it is necessary that this is output to the console:

 15/Alexey 10/Dmitriy 8/Liza 6/Maksim 1/Andrey 
  • sort -n -r < datafile , only sort is GNU's ... - Harry
  • And without GNU'shny ?? - Maxim Bogdanov

1 answer 1

 #include <vector> #include <string> #include <iostream> #include <iomanip> #include <fstream> #include <algorithm> using namespace std; int main() { vector<string> v; ifstream in("data"); string s; while(getline(in,s)) v.push_back(s); sort(v.begin(),v.end(),[](const string& s1, const string& s2) { return stoi(s1) > stoi(s2); }); for(auto s: v) cout << s << endl; } 

For super trees with flaws (checked on Open Watcom 1.9)

 #include <vector> #include <string> #include <iostream> #include <iomanip> #include <fstream> #include <algorithm> using namespace std; bool compare(const string& s1, const string& s2) { return atoi(s1.c_str()) > atoi(s2.c_str()); } int main() { vector<string> v; ifstream in("data"); char buf[256]; while(in.getline(buf,256)) v.push_back(buf); sort(v.begin(),v.end(),compare); for(vector<string>::iterator s = v.begin(); s != v.end(); ++s) cout << s->c_str() << endl; } 
  • Well, you have some very ancient compiler ... Well, now I will write for antiquity. - Harry
  • See for especially the ancients ... - Harry
  • Code :: Blocks 16.01 is it considered ancient?) - Maxim Bogdanov
  • Yes, it worked for the "ancient". Thank you very much. I will understand in detail. - Maxim Bogdanov