W
Wang Tong
I have the following code, which removes a pair (string, int) from the
set. I defined a predicate that returns true if the string value matches.
But I am getting compiler error on the remove_if function:
Here is the code:
typedef pair<string, int> my_pair;
typedef set<my_pair> my_set;
typedef my_set::iterator my_set_p;
class my_pair_eq : public unary_function<my_pair, bool>
{
string s;
public:
explicit my_pair_eq (const string& ss) : s(ss) { }
bool operator() (const my_pair& mp) const {return mp.first == s; }
};
int main(void)
{
my_set mset;
mset.insert(my_pair("one", 1));
mset.insert(my_pair("one", 2));
mset.insert(my_pair("two", 2));
mset.insert(my_pair("one", 3));
mset.insert(my_pair("three", 3));
remove_if(mset.begin(), mset.end(), my_pair_eq("one"));
return 0;
}
set. I defined a predicate that returns true if the string value matches.
But I am getting compiler error on the remove_if function:
Here is the code:
typedef pair<string, int> my_pair;
typedef set<my_pair> my_set;
typedef my_set::iterator my_set_p;
class my_pair_eq : public unary_function<my_pair, bool>
{
string s;
public:
explicit my_pair_eq (const string& ss) : s(ss) { }
bool operator() (const my_pair& mp) const {return mp.first == s; }
};
int main(void)
{
my_set mset;
mset.insert(my_pair("one", 1));
mset.insert(my_pair("one", 2));
mset.insert(my_pair("two", 2));
mset.insert(my_pair("one", 3));
mset.insert(my_pair("three", 3));
remove_if(mset.begin(), mset.end(), my_pair_eq("one"));
return 0;
}