[boost-users]Shared_ptr and std::find
Is it possible to have std::find compare the class that the shared_ptr is pointing too? I have a list of shared_ptr of a class called FundamentalTypes. The class has it's own operator==. I'd like to search the list with std::find using the class's operator==. I've included the class with the key elements I'm referring too. Ryan class FundamentalTypes { public: FundamentalTypes(std::string const& name) : m_name(name) bool operator==(FundamentalTypes const& rhs) { return (rhs.m_name == m_name) ? true : false; } std::string m_name; }; template<typename T> class Type : public FundamentalTypes { Type(std::string const& name) : FundamentalTypes(name) }; //main typedef shared_ptr<FundamentalTypes> sharedTypes typedef std::list<sharedTypes> TypesList sharedTypes value = sharedTypes(new Type<short>("bob")); TypesList m_list; TypesList::iterator itor = std::find(m_list.begin(), m_list.end(), value);
On Thursday 06 August 2009, Ryan McConnehey wrote:
Is it possible to have std::find compare the class that the shared_ptr is pointing too?
It would probably be easier to use std::find_if()
I have a list of shared_ptr of a class called FundamentalTypes. The class has it's own operator==. I'd like to search the list with std::find using the class's operator==. I've included the class with the key elements I'm referring too.
Thanks, it looks like what I need.
It would probably be easier to use std::find_if()
As a side question, the reason I'm storing the classes as shared_ptr was to avoid the copy constructor need to place it in the list. So, if I was to store the classes in a std::list<FundamentalTypes> the class would be constructed. Then a copy constructor would be called to place the object into the list. By storing the class as a pointer I've eliminated the copy constructor call. Is this reasoning correct? Ryan
Frank Mori Hess wrote:
On Thursday 06 August 2009, Ryan McConnehey wrote:
Is it possible to have std::find compare the class that the shared_ptr is pointing too?
It would probably be easier to use std::find_if()
Or see: http://www.boost.org/doc/libs/1_39_0/libs/iterator/doc/indirect_iterator.htm... MyContOfPtrs::iterator itr = std::find( make_indirect_iterator(myContOfPtrs.begin()) , make_indirect_iterator(myContOfPtrs. end()) , AnInstance).base(); note call .base() on the return value from std::find to get the original iterator. Jeff
participants (3)
-
Frank Mori Hess
-
Jeff Flinn
-
Ryan McConnehey