Re: Searching a vector of structs
From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 01/19/05
- Next message: schnitzell: "Problem z fokusem."
- Previous message: KTC: "Re: rand()"
- In reply to: artifice_1_at_hotmail.com: "Searching a vector of structs"
- Next in thread: Alwyn: "Re: Searching a vector of structs"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 19 Jan 2005 21:06:03 +1100
<artifice_1@hotmail.com> wrote in message
news:1106116457.279980.143100@c13g2000cwb.googlegroups.com...
| If you've got a struct like:
|
| struct MyStruct
| {
| int x;
| int y;
|
| bool operator< ( const MyStruct &cmp ) const
| {
| return( x < cmp.x );
| }
| };
|
| and a vector like: std::vector< MyStruct > MyVector;
|
| I can do a sort by saying: std::sort( MyVector.begin(), MyVector.end()
| ); and it will know to compare based on the value of x.
|
| But if I wanted to find one (or all) structs that have a value x equal
| to, say, 10, how do I do that?
|
| I can't do std::equal_range( MyVector.begin(), MyVector.end(), 10 )
| because it doesn't know how to compare 10 against a MyStruct.
|
| If I define a function like:
|
| bool CompareIntToStruct( const int &lhs, const MyStruct &rhs )
| {
| return( lhs < rhs.x );
| }
|
| and then try std::equal_range( MyVector.begin(), MyVector.end(), 10,
| CompareIntToStruct) I get other errors because sometimes (I'm using the
| SGI STL here) it will try to compare with a function call that takes
| parms ( int, MyStruct) but other times it will need the parms to be
| ordered (MyStruct, int) instead.
If it is just a struct with public data as the data to
look for, then a simple for loop with an iterator is
probably the simplest method:
int Wanted( 10 );
std::vector<MyStruct>::const_iterator Idx( V.begin() );
for( Idx; Idx != V.end(); ++Idx )
{
if( Wanted == Idx -> x )
std::cout << "Found one " << std::endl;
else
std::cout << "Not Found " << std::endl;
}
There are other ways too, depending on your needs.
You may choose to use:
1) A conversion operator
2) 'std::find_if' with a suitable predicate
3) 'std::find_if' with 'std::bind2nd' and 'std::equal_to'
4) A simple iterator in a for loop as already shown above
5) Maybe even more I haven't thought of :-)
| I know I can define a Compare( const MyStruct &lhs, const MyStruct &rhs
| ) function, but I don't want to pass an entire struct in (say for
| example I had a huge struct, or if we were dealing with some object
| that does a lot of work or allocates a lot of stuff in the
| constructor). I just want to pass in an int and have it search for
| that.
You have a misconception about how references work, otherwise
you would not have commented on the fact that you have to move
around 'huge' objects.
We use pass by reference semantics for this very reason, to
avoid the copying of large objects.
| Is this possible?
Anything is possible :-)
Cheers.
Chris Val
- Next message: schnitzell: "Problem z fokusem."
- Previous message: KTC: "Re: rand()"
- In reply to: artifice_1_at_hotmail.com: "Searching a vector of structs"
- Next in thread: Alwyn: "Re: Searching a vector of structs"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|