|
-
December 21st, 2002, 10:54 PM
#1
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.
-
December 23rd, 2002, 01:24 AM
#2
#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
-
December 23rd, 2002, 11:58 AM
#3
Nice! Thanks.
What is the purpose of std: tr_fun?
Kuphryn
-
December 23rd, 2002, 12:14 PM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|