कुछ
तरह
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"));
या लैम्ब्डा, अगर सी ++ 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))));
के लिए अनुमान उत्पन्न करती है आपने इसे स्वयं कहा: 'std :: find_if'। आप इसे लैम्ब्डा के साथ भी जोड़ सकते हैं। – chris
क्यों सी ++ 11 के लिए नफरत है? – nikhil
कोई नफरत नहीं है, हम इसे प्यार करते हैं! :) – Narek