std::map find_if condition style confusion

前端 未结 8 1154
星月不相逢
星月不相逢 2021-02-01 23:43

I\'d like to use std::find_if to search for the first element in my map that has a certain value in a specific element of its value structure. I\'m a little confus

8条回答
  •  自闭症患者
    2021-02-02 00:16

    This doesn't have anything to do with std::bind1st or std::bind2nd. First of all, you have to keep in mind that the elements of a map are key-value pairs, in your case std::pair. Then you just need a predicate that compares the x member of the second member of yuch a pair against a specific value:

    struct XEquals : std::unary_function,bool>
    {
        XEquals(int _x)
            : x(_x) {}
        bool operator()(const std::pair &v) const
            { return p.second.x == x; }
        int x;
    };
    

提交回复
热议问题