-
sorting objects
hey all,
how do you sort objects? i can sort the actual object just fine, but doesn't that take up a lot of memory, passing the really big objects back and forth? i tried pointers, but can't find the right syntax. let's say i have the following, or thereabouts:
Code:
class test
{
int x, y, z
//.... constructors and so on
int main(){
test t1(1),
t2(2),
t3(3);//i want to eventually sort the object by the parameter
//... function to sort test's objects
what's the correct syntax for sorting t1, t2, t3?
-
Do you mean sort tests stored within a container, or sort the values within one instance of test?
-
well, i don't know what a container is right now, but this is exactly what i need: instances that need to be sorted at runtime to be used, in that order, later on in the program.
imagine the following instances are baseball players with values for pitching, chatching, and so on. i need the user to sort their batting order. so what i wanted to do was to place pointers in a que rather than objects:
Code:
Player h1(12,3,3,0,0,4,2,3,"Alby Abe", 1);
Player h2(14,1,1,1,1,1,1,1,"Boss",2);
Player h3(1,3,3,0,0,4,2,3,"Chuck up", 3);
Player h4(7,1,1,1,1,1,1,1,"Davy",4);
Player h5(5,3,3,0,0,4,2,3,"Eirn", 5);
Player h6(0,1,1,1,1,1,1,1,"Fred",6);
Player h7(88,3,3,0,0,4,2,3,"Godfry", 7);
Player h8(6,1,1,1,1,1,1,1,"Hippe",8);
Player h9(15,3,3,0,0,4,2,3,"Ian", 9);
Player h10(27,1,1,1,1,1,1,1,"Jack",0);
Player h11(16,3,3,0,0,4,2,3,"Killer", 0);
Player h12(22,1,1,1,1,1,1,1,"Lane",0);
Player h13(3,3,3,0,0,4,2,3,"Matty", 0);
Player h14(11,1,1,1,1,1,1,1,"Ned",0);
Player h15(20,3,3,0,0,4,2,3,"Oris", 0);
(sorry about any typos, i cut my fingers this week and am having trouble typing :( )
-
Sorry, I'm still a bit confused. Do you just want to create a list of players entered by a user or use some pre-defined sorting function to insert the players into a list in some order, such as one of the attributes the players have?
-
:) a little bit of both. i want the user to sort the above instances to his liking. then i want the program to store them, in that order for use latter uin the program. i'm having a difficult time comunicating today, aren't i?
-
i think I know what you want to do but have no idea how to go about doing it.
-
well, i didn't think it would be difficult at all. can't i just point to a object?
-
A simple example -
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct player
{
int pitching;
player (int x):pitching(x){}
bool operator <(const player& rhs)const
{
return pitching < rhs.pitching;
}
friend ostream& operator <<(ostream& os,const player& p)
{
return os << p.pitching;
}
};
int main()
{
player one(55);
player two(125);
player three(81);
vector<player> players;
players.push_back(one);
players.push_back(two);
players.push_back(three);
sort(players.begin(),players.end());
copy(players.begin(),players.end(),ostream_iterator<player>(cout," "));
return 0;
}
If you want to just use pointers you can create a vector of player pointers rather than player objects (you'd have to create a global comparison function that accepts pointers that you can plug into std::sort). A different container (std::list, std::set, etc) may be more suitable depending on how you want to create and maintain the container, and whether you need to frequently re-sotr.