Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Using find_if with vector of pointers ?

Using find_if with vector of pointers ?

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++graphicsdata-structurestutorial
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    squidev
    wrote on last edited by
    #1

    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

    P S A 3 Replies Last reply
    0
    • S squidev

      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

      P Offline
      P Offline
      Pavel Klocek
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S squidev

        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

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        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

        L S 2 Replies Last reply
        0
        • S Stephen Hewitt

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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?

          S 1 Reply Last reply
          0
          • L Lost User

            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?

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Yeah this would work just fine but it's not "adaptable". STL functions such as bind1st and bind2st reply on certain typedefs (metadata if you like) being present to work. The simplest way to supply them is to derive from unary_function or binary_function. Steve

            N 1 Reply Last reply
            0
            • S Stephen Hewitt

              Yeah this would work just fine but it's not "adaptable". STL functions such as bind1st and bind2st reply on certain typedefs (metadata if you like) being present to work. The simplest way to supply them is to derive from unary_function or binary_function. Steve

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              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.

              S 1 Reply Last reply
              0
              • P Pavel Klocek

                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

                S Offline
                S Offline
                squidev
                wrote on last edited by
                #7

                Thanks for the link, I have been looking for such reading material. And the download is free :cool:

                1 Reply Last reply
                0
                • S Stephen Hewitt

                  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

                  S Offline
                  S Offline
                  squidev
                  wrote on last edited by
                  #8

                  Oh that's what I was missing! I have seen a lot of functors examples but they rarely had their own constructor. Thanks!

                  1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    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.

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    Yeah, Boost's Bind and Lambda libraries add considerably to what can be done with STL alone. Steve

                    1 Reply Last reply
                    0
                    • S squidev

                      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

                      A Offline
                      A Offline
                      Axter
                      wrote on last edited by
                      #10

                      The 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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups