By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,150 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,150 IT Pros & Developers. It's quick & easy.

vector sort for high scores

P: 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
Share this Question
Share on Google+
8 Replies


P: 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
Expert 100+
P: 1,764
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

P: 19
Oh ok. We haven't gotten to structs yet in class. I'll research them. Thanks for you help.
Jan 13 '08 #4

intOwnsVoid
P: 11
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

P: 3
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

P: 3
sorry ... please ignore comment infront at the beginning of the sort line
Jan 29 '08 #7

P: 3
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

Expert 100+
P: 849
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

Post your reply

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