Can I easily iterate over the values of a map using a range-based for loop?

后端 未结 4 1220
醉梦人生
醉梦人生 2021-02-03 20:38

Is it possible to iterate over all of the values in a std::map using just a "foreach"?

This is my current code:

std::map

        
4条回答
  •  时光取名叫无心
    2021-02-03 21:12

    The magic lies with Boost.Range's map_values adaptor:

    #include 
    
    for(auto&& i : foo | boost::adaptors::map_values){
      i->bar();
    }
    

    And it's officially called a "range-based for loop", not a "foreach loop". :)

提交回复
热议问题