Using find_if with vector of pointers ?
-
Hi all, I know STL enough to understand containers and iterators but going from Predicates to Unary/Binary and vice versa is a bit unclear to me. I have
class CMyClass { public: // SNIP inline CString GetID() const {return m_csID;} // SNIP protected: CString m_csID; // SNIP };
Now suppose I want an array of CMyClass* and detect whether an object of a specified ID already exist. How can I use find_if ?// SNIP vector myarray; LPCTSTR lpszNewID = _T("NEWID"); if (find_if(myarray.begin(), myarray.end(), IsID(lpszNewID)???) != myarray.end()) // then don't add it a second time
Any idea how to implement and properly use the "IsID(LPCTSTR lpszID)" part? :confused: Thank you -
Hi all, I know STL enough to understand containers and iterators but going from Predicates to Unary/Binary and vice versa is a bit unclear to me. I have
class CMyClass { public: // SNIP inline CString GetID() const {return m_csID;} // SNIP protected: CString m_csID; // SNIP };
Now suppose I want an array of CMyClass* and detect whether an object of a specified ID already exist. How can I use find_if ?// SNIP vector myarray; LPCTSTR lpszNewID = _T("NEWID"); if (find_if(myarray.begin(), myarray.end(), IsID(lpszNewID)???) != myarray.end()) // then don't add it a second time
Any idea how to implement and properly use the "IsID(LPCTSTR lpszID)" part? :confused: Thank youGood book on stl, which you can buy on paper or dowload for free and where you find answer to your question is Thinking In C++ Volume 2: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[^] Pavel
-
Hi all, I know STL enough to understand containers and iterators but going from Predicates to Unary/Binary and vice versa is a bit unclear to me. I have
class CMyClass { public: // SNIP inline CString GetID() const {return m_csID;} // SNIP protected: CString m_csID; // SNIP };
Now suppose I want an array of CMyClass* and detect whether an object of a specified ID already exist. How can I use find_if ?// SNIP vector myarray; LPCTSTR lpszNewID = _T("NEWID"); if (find_if(myarray.begin(), myarray.end(), IsID(lpszNewID)???) != myarray.end()) // then don't add it a second time
Any idea how to implement and properly use the "IsID(LPCTSTR lpszID)" part? :confused: Thank youHere's how I'd do it:
class IsID : public unary_function<CMyClass*, bool> { public: IsID(LPCTSTR pID) : m_pID(pID) {} bool operator()(const CMyClass *pClass) const { return pClass->GetID() == m_pID; } private: LPCTSTR m_pID; };
This code assumes the following (in addition to what you must already have):
#include <functional>
This is called a functor - It has a few important advantages over a normal function: - It's can be called like a function but it can have state. - It's faster - Most compilers can't inline through a function pointer but have no trouble doing do so via
operator()
(the application operator). Steve -
Here's how I'd do it:
class IsID : public unary_function<CMyClass*, bool> { public: IsID(LPCTSTR pID) : m_pID(pID) {} bool operator()(const CMyClass *pClass) const { return pClass->GetID() == m_pID; } private: LPCTSTR m_pID; };
This code assumes the following (in addition to what you must already have):
#include <functional>
This is called a functor - It has a few important advantages over a normal function: - It's can be called like a function but it can have state. - It's faster - Most compilers can't inline through a function pointer but have no trouble doing do so via
operator()
(the application operator). SteveWouldn't a "normal" function object work just as well? E.g.:
struct IsID
{
LPCTSTR id_;
IsID(LPCTSTR id) : id_(id) { }
bool operator()(const CMyClass* p) const
{
return pClass->GetID() == id_;
}
};
}
...
if (find_if(myarray.begin(), myarray.end(), IsID(string)) == myarray.end())
{
// Found
}I am still relatively new to STL, so am keen to learn what advantages deriving from unary_function will give you. Do you have more details and examples?
-
Wouldn't a "normal" function object work just as well? E.g.:
struct IsID
{
LPCTSTR id_;
IsID(LPCTSTR id) : id_(id) { }
bool operator()(const CMyClass* p) const
{
return pClass->GetID() == id_;
}
};
}
...
if (find_if(myarray.begin(), myarray.end(), IsID(string)) == myarray.end())
{
// Found
}I am still relatively new to STL, so am keen to learn what advantages deriving from unary_function will give you. Do you have more details and examples?
Yeah this would work just fine but it's not "adaptable". STL functions such as
bind1st
andbind2st
reply on certaintypedef
s (metadata if you like) being present to work. The simplest way to supply them is to derive fromunary_function
orbinary_function
. Steve -
Yeah this would work just fine but it's not "adaptable". STL functions such as
bind1st
andbind2st
reply on certaintypedef
s (metadata if you like) being present to work. The simplest way to supply them is to derive fromunary_function
orbinary_function
. SteveTrue. I would just add that boost::bind does not require the typedefs, so if you are using boost, this is not an issue.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Good book on stl, which you can buy on paper or dowload for free and where you find answer to your question is Thinking In C++ Volume 2: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html[^] Pavel
-
Here's how I'd do it:
class IsID : public unary_function<CMyClass*, bool> { public: IsID(LPCTSTR pID) : m_pID(pID) {} bool operator()(const CMyClass *pClass) const { return pClass->GetID() == m_pID; } private: LPCTSTR m_pID; };
This code assumes the following (in addition to what you must already have):
#include <functional>
This is called a functor - It has a few important advantages over a normal function: - It's can be called like a function but it can have state. - It's faster - Most compilers can't inline through a function pointer but have no trouble doing do so via
operator()
(the application operator). Steve -
True. I would just add that boost::bind does not require the typedefs, so if you are using boost, this is not an issue.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
Yeah, Boost's Bind and Lambda libraries add considerably to what can be done with STL alone. Steve
-
Hi all, I know STL enough to understand containers and iterators but going from Predicates to Unary/Binary and vice versa is a bit unclear to me. I have
class CMyClass { public: // SNIP inline CString GetID() const {return m_csID;} // SNIP protected: CString m_csID; // SNIP };
Now suppose I want an array of CMyClass* and detect whether an object of a specified ID already exist. How can I use find_if ?// SNIP vector myarray; LPCTSTR lpszNewID = _T("NEWID"); if (find_if(myarray.begin(), myarray.end(), IsID(lpszNewID)???) != myarray.end()) // then don't add it a second time
Any idea how to implement and properly use the "IsID(LPCTSTR lpszID)" part? :confused: Thank youThe find_if can work if you use a good smart pointer that uses value semantics for the comparison. Check out the following link: http://axter.com/smartptr The above smart pointer uses value semantics, so that the pointee is compated instead of the pointer address. Top ten member of C++ Expert Exchange. http://www.experts-exchange.com/Cplusplus