How to use structure binding to print unordered_map in C++

2 Answers

0 votes
#include <iostream>
#include <unordered_map>

void print_map() {
    std::unordered_map<std::string, std::string> colors_map = {
        {"RED", "#FF0000"},
        {"GREEN", "#00FF00"},
        {"BLUE", "#0000FF"} 
    };
    
    
    for (const auto&[color, hex]: colors_map) {
        std::cout << "color: " << color << ", hex: " << hex << '\n';
    }
}

int main(void)
{
    print_map();
}
 
 
 
 
/*
run:
 
color: BLUE, hex: #0000FF
color: GREEN, hex: #00FF00
color: RED, hex: #FF0000
 
*/

 



answered Dec 8, 2023 by avibootz
0 votes
#include <iostream>
#include <unordered_map>

void print_map(std::unordered_map<std::string, std::string> colors_map) {

    for (const auto&[color, hex]: colors_map) {
        std::cout << "color: " << color << ", hex: " << hex << '\n';
    }
}

int main(void)
{
    std::unordered_map<std::string, std::string> colors_map = {
        {"RED", "#FF0000"},
        {"GREEN", "#00FF00"},
        {"BLUE", "#0000FF"} 
    };
    
    print_map(colors_map);
}
 
 
 
 
/*
run:
 
color: BLUE, hex: #0000FF
color: GREEN, hex: #00FF00
color: RED, hex: #FF0000
 
*/

 



answered Dec 8, 2023 by avibootz

Related questions

2 answers 142 views
1 answer 119 views
1 answer 112 views
1 answer 100 views
1 answer 117 views
1 answer 128 views
128 views asked Sep 17, 2022 by avibootz
3 answers 336 views
...