Is there a standard C++ function object for taking apart a std::pair?

前端 未结 6 1595
粉色の甜心
粉色の甜心 2020-12-20 14:06

Does anyone know if there\'s a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I\'ve wished

6条回答
  •  無奈伤痛
    2020-12-20 14:36

    boost::bind is what you look for.

    boost::bind(&std::pair::second, _1); // returns the value of a pair
    

    Example:

    typedef std::map map_type;
    
    std::vector values; // will contain all values
    map_type map;
    std::transform(map.begin(), 
                   map.end(), 
                   std::back_inserter(values), 
                   boost::bind(&map_type::value_type::second, _1));
    

提交回复
热议问题