Thread: Search (Compare) Functor and std::pair :: STL

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Search (Compare) Functor and std::pair :: STL

    Hi.

    How do you definte a comparison function for an container of std::pair other than map? I need to search a container of std::pair. For example:

    Code:
    typedef std::deque<std::pair<std::string, int> > deqStringInt;
    
    deqStringInt example;
    example.push_back(std::pair<"December", 21>);
    
    // How do you search an element?
    
    std::find_if(example.begin(), example.end(), std::bind2nd(???
    
    // Functor
    class Search : std::binary_function<std::pair<std::string, int>, std::string, bool>
    {
    public:
       bool operator()(const std::pair<std::string, int> &lp, const std::string &rp)
       {
          return lp->first == rp;
       }
    };
    Thanks,
    Kuphryn

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I wrote up a very sloppy solution. I didn't use any functors in mine though. They were given me some beef with v$ so I just did it with a standard compare function. Here is my sample..

    Code:
    #include <functional>
    #include <algorithm>
    #include <utility>
    #include <iostream>
    #include <string>
    #include <deque>
    
    // Functor
    // Not used in my function..
    class Search : public std::binary_function<std::pair<std::string, int>, std::string, bool>
    {
      public:
      
         bool operator()(std::pair<std::string, int> &lp, std::string &rp)
         {
            return true;
         }
    };
    
    typedef std::deque<std::pair<std::string, int> > deqStringInt;
    typedef struct std::pair<std::string, int> PAIR;
    
    // Here is my comparison function
    bool simple( std::pair< std::string, int > lp, std::string rp )
    {
      return !strcmp( lp.first.c_str(), rp.c_str() );
    }
    
    int main( void )
    {
      deqStringInt example;
      example.push_back( PAIR("December", 21) );
    
      // How do you search an element?
    
      deqStringInt::iterator i = std::find_if( example.begin(), 
                    example.end(), 
                    std::bind2nd( std::ptr_fun(simple), "December" ) );
                    
      if( i != example.end() )
      {
        std::cout << "Found" << std::endl;
      }
      else
      {
        std::cout << "Not Found :(" << std::endl;
      }
    
      return 0;
    }
    Tomorrow when I get some sleep I will post up the other version using functors if you'd like.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Nice! Thanks.

    I have some questions about your solution. It makes use of something I do not know exists. Anyways, I want to rest too.

    Yes, I look forward to possibly a working solution using a functor just to see the use of functor and std:air.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Update:

    Here is the code using the function object:

    Code:
    #include <functional>
    #include <algorithm>
    #include <utility>
    #include <iostream>
    #include <string>
    #include <deque>
    
    // Functor
    class Search : public std::binary_function<std::pair<std::string, int>, std::string, bool>
    {
      public:
      
        bool operator()(std::pair< std::string, int > lp, std::string rp) const
         {
            return !strcmp( lp.first.c_str(), rp.c_str() );
         }
    };
    
    typedef std::deque<std::pair<std::string, int> > deqStringInt;
    typedef struct std::pair<std::string, int> PAIR;
    
    int main( void )
    {
      deqStringInt example;
      example.push_back( PAIR("December", 21) );
    
      // How do you search an element?
    
      deqStringInt::iterator i = std::find_if( example.begin(), 
                    example.end(), 
                    std::bind2nd( Search(), "December" ) );
                    
      if( i != example.end() )
      {
        std::cout << "Found" << std::endl;
      }
      else
      {
        std::cout << "Not Found :(" << std::endl;
      }
    
      return 0;
    }
    Ugh, I'm such an insomniac. It's 4:30am and I'm messing with the STL. I'm such a loser. Anyways I can't even remember what I changed yours from to get it to work. I know you didn't specify the "public" inheritance in yours. That was a problem. Also the overloaded operator needed to have a const for the data members. There are some subtle things that needed to be done. If you have any questions on what I did or how I arrived at a certain solution PM me or just post it on this thread. Hope it helped you.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Nice!!! Thanks.

    Here is the final working solution I implemented using your model.

    Code:
    class Searc : std::binary_function<pairStringInt, std::string, bool>
       {
       public:
          bool operator()(const pairStringInt &lp, const std::string &rp) const
          {
             return (lp.first == rp);
          }
       };
    
    std::find_if(data.begin(), data.end(), std::bind2nd(Search(), theString))
    Kuphryn

Popular pages Recent additions subscribe to a feed