C
Christian Chrismann
Hi,
I've a question on the STL find algorithm:
My code:
int main( void )
{
vector< ClassA* > myVector;
ClassA *ptrElement1 = ...;
myVector.push_front( ptrElement1 );
...
ClassA *ptrElement2 = *(myVector.begin());
vector< ClassA* >::iterator it =
find( myVector.begin(), myVector.end(), ptrElement2 );
...
}
What is used here in the 'find' algorithm for finding 'ptrElement2'?
Since 'ptrElement2' is just a pointer (so it's a variable storing an
address), I assume that each element (also pointers) in the list is
compared with 'ptrElement2' by using the operator== on the
addresses they represent, i.e. it is checked if
(address stored in any element in the list) ==
(address stored in 'ptrElement2')
Is my assumption correct?
However, this is probably rarely wanted. Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the 'find' algorithm and additionally make sure
that ClassA provides a proper operator==. Right?
Regards,
Chris
I've a question on the STL find algorithm:
My code:
int main( void )
{
vector< ClassA* > myVector;
ClassA *ptrElement1 = ...;
myVector.push_front( ptrElement1 );
...
ClassA *ptrElement2 = *(myVector.begin());
vector< ClassA* >::iterator it =
find( myVector.begin(), myVector.end(), ptrElement2 );
...
}
What is used here in the 'find' algorithm for finding 'ptrElement2'?
Since 'ptrElement2' is just a pointer (so it's a variable storing an
address), I assume that each element (also pointers) in the list is
compared with 'ptrElement2' by using the operator== on the
addresses they represent, i.e. it is checked if
(address stored in any element in the list) ==
(address stored in 'ptrElement2')
Is my assumption correct?
However, this is probably rarely wanted. Usually one want to compare
the objects (and not their addresses). To achieve this, one must
dereference ptrElement2 in the 'find' algorithm and additionally make sure
that ClassA provides a proper operator==. Right?
Regards,
Chris