Re: STL: vector with pointers
"Frank" <jerk@gmx.de> ha scritto nel messaggio
news:5e6b74c9-c97e-42fe-973c-1b3b432646a5@l9g2000yqi.googlegroups.com...
here's a question concerning the STL class "vector".
I have a vector containing pointers to a custom made class, say
vector <CMyClass *>. Now if I want to use the algorithm functions,
say, "find()" I can't because I'd need an equals operator for
two CMyClass* objects which I don't have. Of course I'd
like to find a certain CMyClass object, not a certain pointer.
Now my first approach is to define a class derived
from CMyClass* - is that possible? Or should I do
something else?
What I would do is to define a functor (or "function object") to do the
comparison using pointers to CMyClass instances.
The functor can be derived from std::binary_function< CMyClass *, CMyClass
*, bool >, because your comparison function takes two CMyClass pointers as
input, and returns a boolean value (true if the instances pointed are equal,
false if they are not).
Then you can use std::find_if, passing the comparison helper as third
parameter.
This a simple C++ code I wrote (it compiles fine on VS2008 SP1, and seems to
work...):
<code>
//////////////////////////////////////////////////////////////////////////
// Test.cpp
//
// Includes
//
#include <vector> // vector container
#include <iostream> // cout
#include <algorithm> // find_if
#include <functional> // binder2nd
//
// Test class
//
class CMyClass
{
public:
// Creates an instance of the class with specified ID
explicit CMyClass(int id)
: id_( id )
{
}
// Returns instance ID
int ID() const
{
return id_;
}
private:
int id_;
};
//
// Functor to compare instances of CMyClass (specified by pointers)
//
struct MyClassComparer
// This is a binary function having CMyClass pointers as input,
// and returning 'bool'.
: public std::binary_function< CMyClass *, CMyClass *, bool >
{
// Does the comparison
bool operator()( const CMyClass * lhs, const CMyClass * rhs ) const
{
// They are equal if they have the same ID
return lhs->ID() == rhs->ID();
}
};
//
// TEST
//
int main()
{
using std::vector;
using std::find_if;
using std::bind2nd;
using std::cout;
using std::endl;
//
// Vector of pointers to CMyClass
//
typedef vector< CMyClass * > MyClassPtrVector;
MyClassPtrVector container;
container.push_back( new CMyClass(1) );
container.push_back( new CMyClass(10) );
container.push_back( new CMyClass(3) );
container.push_back( new CMyClass(4) );
// Element to find
CMyClass * elementToFind = new CMyClass(3);
//
// Search element using find_if
//
MyClassPtrVector::iterator result;
result = find_if(
container.begin(), container.end(),
bind2nd( MyClassComparer(), elementToFind )
);
cout << "Element with ID = " << elementToFind->ID() << " was ";
if ( result != container.end() )
{
cout << "found !" << endl;
}
else
{
cout << "not found." << endl;
}
//
// Cleanup
//
delete elementToFind;
elementToFind = NULL;
MyClassPtrVector::iterator it;
for (it = container.begin(); it != container.end(); ++it)
{
delete *it;
*it = NULL;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
</code>
HTH,
Giovanni