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.

40,561 questions

52,726 answers

573 users

How to use map in C++

4 Answers

0 votes
#include <iostream>
#include <string>
#include <map>
 
void printMap(std::map<std::string, int> mp) {
  for (auto &s: mp) {
        std::cout << s.first << ": " << s.second << '\n';
  }    
}
 
int main ()
{
    std::map<std::string,int> mp = {
                    { "c++", 23 },
                    { "c", 6 },
                    { "python", 87 },
                    { "java", 980 } };
 
    printMap(mp);
 
    return 0;
}
 
 
 
/*
run:
 
c: 6
c++: 23
java: 980
python: 87
 
*/

 



answered Apr 13, 2020 by avibootz
edited Apr 13, 2020 by avibootz
0 votes
#include <iostream>
#include <string>
#include <map>
 
void printMap(std::map<std::string, int> mp) {
  for (auto &s: mp) {
        std::cout << s.first << ": " << s.second << '\n';
  }    
}
 
int main ()
{
    std::map<std::string,int> mp = {
                    { "c++", 23 },
                    { "c", 6 },
                    { "python", 87 },
                    { "java", 980 } };
 
    mp.at("c++") = 1000;
    mp.at("java") = 2000;
 
    printMap(mp);
 
    return 0;
}
 
 
 
/*
run:
 
c: 6
c++: 1000
java: 2000
python: 87
 
*/

 



answered Apr 13, 2020 by avibootz
edited Apr 13, 2020 by avibootz
0 votes
#include <iostream>
#include <string>
#include <map>
 
void printMap(std::map<char, int> mp) {
  for (auto &s: mp) {
        std::cout << s.first << ": " << s.second << '\n';
  }    
}
 
int main ()
{
    std::map<char, int> mp;
 
    mp['a'] = 1;
    mp['x'] = 7;
    mp['r'] = 9;
 
    printMap(mp);
 
    return 0;
}
 
 
 
/*
run:
 
a: 1
r: 9
x: 7
 
*/

 



answered Apr 13, 2020 by avibootz
edited Apr 13, 2020 by avibootz
0 votes
#include <iostream>
#include <string>
#include <map>

void printMap(std::map<char, int> mp) {
   for (std::map<char,int>::iterator it = mp.begin(); it != mp.end(); it++)
        std::cout << it->first << ": " << it->second << '\n';
}

int main ()
{
    std::map<char, int> mp;

    mp['a'] = 1;
    mp['x'] = 7;
    mp['r'] = 9;

    printMap(mp);

    return 0;
}



/*
run:

a: 1
r: 9
x: 7

*/

 



answered Apr 13, 2020 by avibootz

Related questions

1 answer 149 views
149 views asked May 6, 2018 by avibootz
1 answer 129 views
1 answer 221 views
1 answer 176 views
1 answer 42 views
1 answer 76 views
1 answer 68 views
...