Use of for_each on map elements

前端 未结 11 733
北荒
北荒 2021-01-30 10:29

I have a map where I\'d like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associativ

11条回答
  •  清歌不尽
    2021-01-30 11:19

    Here is an example of how you can use for_each for a map.

    std::map map;
    
    map.insert(std::pair(1, 2));
    map.insert(std::pair(2, 4));
    map.insert(std::pair(3, 6));
    
    auto f = [](std::pair it) {std::cout << it.first + it.second << std::endl; };
    std::for_each(map.begin(), map.end(), f);
    

提交回复
热议问题