Home Forums Gamescan Chat42 About
* Login   * Register * FAQ    * Search
It is currently Mon 10-13-2025 11:18PM

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: list of a struct
PostPosted: Tue 11-30-2004 2:12AM 
Offline
Cookie Monster
Cookie Monster
User avatar

Joined: Mon 05-12-2003 6:27PM
Posts: 423
Location: Rolla

Source: CompSci Building
Code:
struct chest{
  char breast;
  int tit;  //inchs in diameter
}

void main(){

  struct chest hoe;
  std::list<struct chest> beoches;

  hoe->breast = 'c';
  hoe->tit = 2;

  beoches.push_back(hoe);

  beoches.sort();
}


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.

thanks,
brandito


Top
 Profile  
    
 Post subject: Re: list of a struct
PostPosted: Tue 11-30-2004 12:01PM 
Offline
Brigadier General

Joined: Tue 01-22-2002 12:35PM
Posts: 1057
Location: Shawnee Mission, KS

Source: Off Campus
Brandito wrote:
Code:
struct chest{
  char breast;
  int tit;  //inchs in diameter
}

void main(){

  struct chest hoe;
  std::list<struct chest> beoches;

  hoe->breast = 'c';
  hoe->tit = 2;

  beoches.push_back(hoe);

  beoches.sort();
}


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.


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Thu 12-02-2004 3:22PM 
Offline
Cookie Monster
Cookie Monster
User avatar

Joined: Mon 05-12-2003 6:27PM
Posts: 423
Location: Rolla

Source: TJ Hall
that helps a little... but I still don't know how to use the sort function like I want. I tried googling but came up short.


Top
 Profile  
    
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 12 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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group