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

后端 未结 4 1290
醉梦人生
醉梦人生 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:30

    From C++1z/17, you can use structured bindings:

    #include 
    #include 
    #include 
    
    int main() {
       std::map m;
    
       m[1] = "first";
       m[2] = "second";
       m[3] = "third";
    
       for (const auto & [key, value] : m)
          std::cout << value << std::endl;
    }
    

提交回复
热议问题