Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,945 questions

51,887 answers

573 users

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 119 views
1 answer 102 views
1 answer 89 views
1 answer 79 views
1 answer 86 views
1 answer 114 views
114 views asked Sep 17, 2022 by avibootz
3 answers 290 views
...