D
Daz
Hello people!
(This post is best viewed using a monospace font).
I need to create a class, which holds 4 elements:
std::string ItemName
int Calories
int Weight
int Density
I need to be able to create about 70 instances of this class, each with
different properties, then I need to sort them three times:
First, by calories. Then do some stuff.
Then by Weight. Then do more stuff.
And finally by Density. After which, I do more stuff.
It was suggested to me yesterday that I should use std::map in order to
sort the data, but I can't see how it would work. For example:
C W D <--(Pure coincidence)
"Fries"------10---159--45-
"Berry"------50---99---65-
"Ice Cream"--90---45---87-
"Hamburger"--4----76---23-
If sorted by Weight (W) should yield:
C W D
"Ice Cream"--90---45---87-
"Berry"------50---99---65-
"Hamburger"--4----76---23-
"Fries"------10---159--45-
I need to sort it by Density and Calories at some point too, yet the
rows need to stay intact.
No, my program isn't really _that_ lame, but that is the one of it's
simplest functions.
If I really can use a std::map, How would I go about doing it? I know I
am missing something really obvious here, but I have spent more hours
than anyone should pondering over this seemingly simple question, and
have not stumbled across any answers.
My next question, is:
Is there any easy way to create 70 instances of my class dynamically,
and iterate through them?
By this I mean, do I have to do:
BBObject Obj1("Berry",50,99,65);
BBObject Obj2("Fries",10,159,45);
BBObject Obj3("Hamburger",4,76,23);
....For 70 objects?
If so, then fair enough. However, if I can do this in a better way, and
somehow make a list so that I can iterate through the object, it would
be fantastic.
The only catch is, that the list order will need to change as I do the
sorting.
N.B. I do not want the code created for me. I am desparately trying to
learn what I can and can't do, along with how to do it. Just when I
think I understand a concept, I am always proved wrong by my own code.
If anyone can offer any advice on the best way to get the results I
need, I would very much appreciate it. Also, if anyone can straighten
me out abut anything I am trying to do which cannot be done, I would
also appreciate it.
Thanks everyone.
Daz
(This post is best viewed using a monospace font).
I need to create a class, which holds 4 elements:
std::string ItemName
int Calories
int Weight
int Density
I need to be able to create about 70 instances of this class, each with
different properties, then I need to sort them three times:
First, by calories. Then do some stuff.
Then by Weight. Then do more stuff.
And finally by Density. After which, I do more stuff.
It was suggested to me yesterday that I should use std::map in order to
sort the data, but I can't see how it would work. For example:
C W D <--(Pure coincidence)
"Fries"------10---159--45-
"Berry"------50---99---65-
"Ice Cream"--90---45---87-
"Hamburger"--4----76---23-
If sorted by Weight (W) should yield:
C W D
"Ice Cream"--90---45---87-
"Berry"------50---99---65-
"Hamburger"--4----76---23-
"Fries"------10---159--45-
I need to sort it by Density and Calories at some point too, yet the
rows need to stay intact.
No, my program isn't really _that_ lame, but that is the one of it's
simplest functions.
If I really can use a std::map, How would I go about doing it? I know I
am missing something really obvious here, but I have spent more hours
than anyone should pondering over this seemingly simple question, and
have not stumbled across any answers.
My next question, is:
Is there any easy way to create 70 instances of my class dynamically,
and iterate through them?
By this I mean, do I have to do:
BBObject Obj1("Berry",50,99,65);
BBObject Obj2("Fries",10,159,45);
BBObject Obj3("Hamburger",4,76,23);
....For 70 objects?
If so, then fair enough. However, if I can do this in a better way, and
somehow make a list so that I can iterate through the object, it would
be fantastic.
The only catch is, that the list order will need to change as I do the
sorting.
N.B. I do not want the code created for me. I am desparately trying to
learn what I can and can't do, along with how to do it. Just when I
think I understand a concept, I am always proved wrong by my own code.
If anyone can offer any advice on the best way to get the results I
need, I would very much appreciate it. Also, if anyone can straighten
me out abut anything I am trying to do which cannot be done, I would
also appreciate it.
Thanks everyone.
Daz