类似
typedef std::pair<std::string, std::pair<int, int> > pair_t;
struct Predicate : public std::unary_function<pair_t, bool>
{
public:
Predicate(const std::string& s):value(s) { }
result_type operator () (const argument_type& pair)
{
return pair.first == value;
}
private:
std::string value;
};
std::vector<pair_t>::const_iterator pos = std::find_if(cont.begin(), cont.end(),
Predicate("ABC"));
或 lambda,如果是 C++11。
auto pos = std::find_if(cont.begin(), cont.end(),
[](const std::pair<std::string, std::pair<int, int>>& pair)
{
return pair.first == "ABC";
});
真的,有一种不好的方法来做这样的事情,没有结构。
typedef std::pair<std::string, std::pair<int, int> > pair_t;
namespace std {
template<>
bool operator ==<> (const pair_t& first, const pair_t& second)
{
return first.first == second.first;
}
}
std::vector<pair_t>::const_iterator pos = std::find_if(cont.begin(), cont.end(),
std::bind2nd(std::equal_to<pair_t>(), std::make_pair("ABC", std::make_pair(1, 2))));