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

前端 未结 6 1604
粉色の甜心
粉色の甜心 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 often used to adapt std::map containers for use with algorithms. Here is an example:

    void print_string(const std::string& s) {
      std::cout << s << '\n';
    }
    
    
    std::map my_map;
    my_map[0]="Boost";
    my_map[1]="Bind";
    
    
    std::for_each(my_map.begin(), my_map.end(),
                  boost::bind(&print_string, boost::bind(
                  &std::map::value_type::second,_1)));
    

提交回复
热议问题