How to iterate map using auto in C++

1 Answer

0 votes
#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> mp = {{1, "c++"}, {2, "games"}, {3, "desktop"}, {4, "programming"}};

    for (const auto& pair : mp) {
        std::cout << pair.first << ": " << pair.second << std::endl;
    }
}



/*
run:

1: c++
2: games
3: desktop
4: programming

*/

 



answered Dec 11, 2024 by avibootz

Related questions

2 answers 187 views
1 answer 154 views
1 answer 192 views
1 answer 155 views
1 answer 227 views
1 answer 109 views
109 views asked Feb 26, 2023 by avibootz
...