std::map find_if condition style confusion

前端 未结 8 1056
星月不相逢
星月不相逢 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-01 23:49

    You can use a lambda function

    int val = ...;
    auto it = std::find_if(myMap.begin(), myMap.end(), 
       [val](const std::pair<int, ValueType> & t) -> bool { 
          return t.second.x == val;
       }
    );
    

    But as Kirill V. Lyadvinsky answer suggests the "first" element may not be what you expect.

    0 讨论(0)
  • 2021-02-01 23:56
    struct Pred
    {
        Pred(int x) : x_(x) { }
        bool operator()(const std::pair<int, ValueType>& p)
        {
            return (x_ == p.second.x);
        }
    private:
        int x_;
    };
    
    ... = std::find_if(myMap.begin(), myMap.end(), Pred(NUMBER));
    
    0 讨论(0)
  • 2021-02-01 23:57

    using Boost.Bind and Boost.Lambda:

    ...
    #include <boost/bind.hpp>
    #include <boost/lambda/lambda.hpp>
    ...
    typedef std::map<int, ValueType> MapType;
    ...
    MapType::iterator pos = std::find_if(myMap.begin(), myMap.end(), 
        boost::bind(&ValueType::y, boost::bind(&MapType::iterator::value_type::second, _1)) == magic_number);
    
    0 讨论(0)
  • 2021-02-02 00:03

    Building on all the answers above I cheat by using decltype with C++11 semantics.

    auto beg_ = myMap.begin();
    auto end_ = myMap.end();
    auto it = find_if(beg_, end_,
        [&some_val](decltype(*beg_) & vt) {
            return vt.second == some_val;});
    if (end_ != it) {
        auto key_found = (*it).first;
    } else {
        // throw error not found.
    }
    
    0 讨论(0)
  • 2021-02-02 00:12

    Elements in the map are not sorted by value, they are sorted according to the key. So the phrase "the first element" has not much sense.

    To find some element (not the first) that has x equal to some value you can write the functor as follows:

    struct check_x
    {
      check_x( int x ) : x_(x) {}
      bool operator()( const std::pair<int, ValueType>& v ) const 
      { 
        return v.second.x == x_; 
      }
    private:
      int x_;
    };
    

    Then use it as follows:

    // find any element where x equal to 10
    std::find_if( myMap.begin(), myMap.end(), check_x(10) );
    
    0 讨论(0)
  • 2021-02-02 00:12

    For the lazy, use a C++17 auto lambda, then you don't need to be verbose with the type.

    const auto it = std::find_if(myMap.begin(), myMap.end(), [&val](const auto &it) { 
          return it.second.x == val; // Comparing with the object
       }
    );
    
    0 讨论(0)
提交回复
热议问题