C++ Loop through Map

前端 未结 5 699
抹茶落季
抹茶落季 2020-11-30 16:56

I want to iterate through each element in the map without knowing any of its string-int values or keys.

What I have so far:



        
5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 17:31

    As @Vlad from Moscow says, Take into account that value_type for std::map is defined the following way:

    typedef pair value_type
    

    This then means that if you wish to replace the keyword auto with a more explicit type specifier, then you could this;

    for ( const pair &p : table ) {
       std::cout << p.first << '\t' << p.second << std::endl;
    } 
    

    Just for understanding what auto will translate to in this case.

提交回复
热议问题