I have two questions...
1) for making a list of structs I do add the word struct between the < > right?
2) how do I use sort() on a struct list? I want to sort by my int value, tit in this example.
I have two questions... 1) for making a list of structs I do add the word struct between the < > right?
Not in C++. Just say
Code:
std::list<chest> beoches;
In C you would have to include the struct (or do the namespace thing...), but in C you probably wouldn't be able to use the STL classes and probably wouldn't have templates. I say "probably" because I don't keep up with the latest and greatest in C.
The "namespace thing" would be something like
Code:
typedef struct CHEST { char breast; int tit; } chest;
...and from then on when you wanted that object you'd just say
Code:
chest Bosom;
or something. Don't do that in C++. At the very least it's unnecessary and at worst you'll confuse the compiler.
Quote:
2) how do I use sort() on a struct list? I want to sort by my int value, tit in this example.
This answer assumes that sort() is actually a valid method for std::list. I'll take your word that it is, but I haven't looked it up myself.
You have to provide a compare operation. It's a really weird way of doing it. IIRC, you have to do something like...
Code:
struct GreaterThan { bool operator () (const Chest& a, const Chest& b) { return (a.tit > b.tit); } };
Then you have to feed that struct as a template parameter somewhere, I think. Make sure that it's actually doing the comparison that the container class expects. Look on a STL website for more info; here's SGI's:
http://www.sgi.com/tech/stl/
Dr Hilgers' CS236 site has an example on how to use the comparison struct with the STL hash table.
http://web.umr.edu/~hilgers/classes/CS236/frame.html Hit "program assignments" and pick program 7. About a third of the way down, under section "The Symbol Table" he links to some example code.
Users browsing this forum: No registered users and 6 guests
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum