std::map find_if condition style confusion

前端 未结 8 1057
星月不相逢
星月不相逢 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:12

    If you want to search also in values then may be better to use Boost Bimap in order not to be slow?

    0 讨论(0)
  • 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<int,ValueType>. 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<std::pair<int,ValueType>,bool>
    {
        XEquals(int _x)
            : x(_x) {}
        bool operator()(const std::pair<int,ValueType> &v) const
            { return p.second.x == x; }
        int x;
    };
    
    0 讨论(0)
提交回复
热议问题