stl list containing objects

To cut a long story short, I'm using

list <helicopter> heliList; //list for helicopters

^this stl list to store objects of the type given above. Can anyone point me to snippets/examples where once on the list, the object has its details displayed, and used/played around with in general?

Obviously I'm asking for quite a specific thing, so I genuinely appreciate any input. Many thanks.
Thank you albatross.

Rather than start a new thread, Ill try and stay in this one. I now need to use a user defined search criteria ( the serial number of the helicopter ) to find the object in the list and delete it.

I believe this involves overloading an operator? but that's far as it goes. Any ideas?
You can use the algoritmh find_if for this:

1
2
3
4
5
6
7
8
9
10
bool mySearchCriteria(const helicopter& heli)
{ return heli1.isApprovedByCIA(heli2); } // put whatever you use for your search ;)

...

list<helicopter>::iterator h = std::find_if ( heliList.begin(), heliList.end(), mySearchCriteria );
if ( h != heliList.end() )
{ // found a helicopter
    helicopter helicopter = *h;
}



For uber-performance, try to look for "functors" (which are classes but called as if they were functions).

Ciao, Imi.

BTW: If your list is sorted, you can use a binary search. The uninformative named function "std::lower_bound" performs a binary search.

Ciao, Imi.
Last edited on
Cheers Imi!

Am I right in thinking the above code could go straight into my search() function?

edit: the search function just has to locate the helicopter and cout its details, nothing more.

Sorry.

Think I may be confused. I'm trying to prompt the user for the serial, which is stored in a variable. Then if this number is found as a helicopters serial the details of that helicopter are printed. This isn't the right way of thinking is it?
Last edited on
You could overload operator<< to print the helicopter details. I would not add cout statements to the search function. That is not a good idea.

Take a look at this.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8
Topic archived. No new replies allowed.