473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vector sort for high scores

19
Hi, guys. I'm writing a little game, and I want to include a high scores list.
I've got everything coded except for the sorting part. I want to use the std::sort() function, but I'm not quite sure how I could get it to work with the data in my vector.

Here is a sample high score file in which each line is inputted into a vector element. I want to sort by the score:

Expand|Select|Wrap|Line Numbers
  1. 2345     toefraz     day time year
  2. 1009     toe           day time year
  3. 8794     toejamb    day time year
  4. ...
Any ideas?
Jan 13 '08 #1
8 3957
toefraz
19
I sort of got it working by using
Expand|Select|Wrap|Line Numbers
  1. sort(v.begin(), v.end(), greater<string>());
but it is sorting the scores like this:

Expand|Select|Wrap|Line Numbers
  1. 975       toefraz   Sun Jan 13 03:33:01 2008
  2. 550       toefraz   Sun Jan 13 04:07:07 2008
  3. 440       toefraz   Sun Jan 13 04:19:52 2008
  4. 330       toefraz   Sun Jan 13 04:26:21 2008
  5. 323       toefraz   Sun Jan 13 03:33:01 2008
  6. 25        toefraz   Sun Jan 13 03:34:24 2008
  7. 220       toefraz   Sun Jan 13 04:36:15 2008
  8. 220       toefraz   Sun Jan 13 04:06:01 2008
  9. 1230      toefraz   Sun Jan 13 03:33:01 2008
  10. 1090      toefraz   Sun Jan 13 03:33:01 2008
whereas I need them to be sorted by numerical value. I'm pretty sure it's because i'm using strings and that's how they sort. How would I go about sorting them properly
Jan 13 '08 #2
Savage
1,764 Expert 1GB
You will need to create a simple struct,containing hi-score,name,and date(and other information you desire).After that you would create a vector of this structs and sort them by hi-score member.Offcourse you would need to write your own compare function and pass it as the third argument.
Jan 13 '08 #3
toefraz
19
Oh ok. We haven't gotten to structs yet in class. I'll research them. Thanks for you help.
Jan 13 '08 #4
The STL (Standard Template Library) defines a group of algorithms that allow you to manipulate elements in containers through iterators.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm> // Added to be able to use the STL algorithms
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.     // This is how you define an iterator in this case i made it constant
  10.     vector<int>::const_iterator iter; 
  11.  
  12.     cout << "Creating a list of scores.\n";
  13.     // declaring a vector named scores that contain integer object elements
  14.     vector<int> scores; 
  15.     scores.push_back (4500);
  16.     scores.push_back (3500);
  17.     scores.push_back (234);
  18.  
  19.     cout << "Printint the list of scores.\n";
  20.     for (iter = scores.begin(); iter != scores.end(); ++iter)
  21.         cout << *iter << endl;
  22.  
  23.     cout << "\n\nSorting scores.\n";
  24.     sort (scores.begin(), scores.end());
  25.     for (iter = scores.begin(); iter != scores.end(); ++iter)
  26.         cout << *iter << endl;
  27.  
  28.     system("PAUSE");
  29.     return (0);
  30. }
  31.  
Jan 13 '08 #5
You will need to create a simple struct,containing hi-score,name,and date(and other information you desire).After that you would create a vector of this structs and sort them by hi-score member.Offcourse you would need to write your own compare function and pass it as the third argument.
Hi, i think i tried to do what you mentioned but i am getting errors:

// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

class tryClass
{
public:
int a, b;
};
int main ()
{
tryClass *temp_Obj;

vector<tryClass> myvector;
vector<tryClass>::iterator it;

for(int i=1; i<= 8; i++)
{
temp_Obj = new(tryClass);
(*temp_Obj).a = 100 - i;
(*temp_Obj).b = i+10;
myvector.push_back(*temp_Obj);
}

// using function as comp
//sort (myvector.begin(), myvector.end(), myfunction);

// print out content:
cout << "myvector contains:" << endl;
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " (" << (*it).a << ", " << (*it).b << ")" << endl;

cout << endl;
return 0;
}

Any suggestions?
Jan 29 '08 #6
sorry ... please ignore comment infront at the beginning of the sort line
Jan 29 '08 #7
phew, ok...i think i got it...(was making a lot of stupid mistakes!!)
below is what i think works....

// sort algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class tryClass
{
public:
int a, b;
};

bool myfunction (tryClass& left, tryClass& right) { return ( left.a < right.a ); }

int main ()
{
tryClass *temp_Obj;
vector<tryClass> myvector;
vector<tryClass>::iterator it;

for(int i=1; i<= 8; i++)
{
temp_Obj = new(tryClass);
(*temp_Obj).a = 100 - i;
(*temp_Obj).b = i+10;
myvector.push_back(*temp_Obj);
}

// using function as comp
sort (myvector.begin(), myvector.end(), myfunction);

// print out content:
cout << "myvector contains:" << endl;
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " (" << (*it).a << ", " << (*it).b << ")" << endl;
cout << endl;

return 0;
}
Jan 29 '08 #8
Laharl
849 Expert 512MB
That looks like it should work, though I have one comment: Every time you use new, get in the habit of using delete (or delete[] for arrays) to de-allocate the memory.
Jan 29 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Allan Bruce | last post by:
Hi there, I am reading through accelerated c++, and have just finshed chapter 3. My question is what sorting algorithm is used on vectors? e.g. if I had: std::vector<int> scores; // adding...
16
by: Kitty | last post by:
Hi, everyone. Given a vector<int>, what is the fastest way to find out whether there is a repeated element in it? The result is just "true" or "false". Thanks. Kitty
0
by: asrock | last post by:
i have prepared a code in c++ to save high scores and it is working well when i compile it in turbo c++and run the code . but when i exit from the turbo c++ , the file to which i saved the high...
0
by: drkplaya | last post by:
How do you push_back a vector in a vector? I have the following: vector<string> &names, vector<vector<int> > &scores while(fin.eof) { fin >> temp names.push_back(temp) while(fin.eof)...
0
by: marick66 | last post by:
I'm trying to write a program that takes numbers from a text file and then sorts them first by name and then by the average. When I go to compile I get 3 overload errors and I can't figure out where...
4
by: kyle christian | last post by:
I am trying to save the high scores of a game I made. Now Im stumped on trying to search through the file. Should I use a string, array, or one of the STL containers to manipulate the information...
1
by: saytri | last post by:
I am doing a game, where a user enters his name after playing the game. This name together with the percentage score are being written (stored) in a textfile called players.txt I am however trying to...
3
by: hamishmcgee | last post by:
Ok, so for a project at university I have to create a High Score table in C++ with Visual Studio. Just to let you know this is my first year at university and also my first time ever learning C++....
2
by: makita18v | last post by:
So my program works all right, but I can't read my information from a file into my vector, what am I doing wrong? vector<char> grades; grades.push_back ('A'); grades.push_back ('B');...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.