CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2002
    Posts
    5,757

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

    Hi.

    How do you definte a comparison function for an container of std:air other than map? I need to search a container of std:air. 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
    Last edited by kuphryn; December 21st, 2002 at 10:57 PM.

  2. #2
    Join Date
    Dec 2002
    Posts
    287
    #include <map>
    #include <deque>
    #include <algorithm>
    #include <string>
    #include <iostream>

    using namespace std;

    typedef pair<string, int> stringIntPair;
    typedef deque<stringIntPair> deqStringInt;

    bool isEq(stringIntPair a_pair, string a_string)
    {
    return (a_pair.first == a_string);
    }
    void f1()
    {


    deqStringInt example;
    example.push_back(stringIntPair("December", 21));

    deqStringInt::iterator iter1 = find_if(example.begin(),
    example.end(),
    bind2nd(ptr_fun(isEq), string("December")));

    if (iter1 != example.end())
    cout << iter1->first << endl;
    else
    cout << "No value" << endl;

    }

    Dan

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Nice! Thanks.

    What is the purpose of std:tr_fun?

    Kuphryn

  4. #4
    Join Date
    Dec 2002
    Posts
    287

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured