How insert a element in a map? help me

Hi, i am difficults in insert elements in a map, because i have change of the code of set to map:



//set<string> h;
map<string> h; I don't know if have to put map<string, int> h for example!

h.insert("HF");
h.insert("GG");
h.insert("GL");

help me, please!
A map requires you to give it 2 types - the key type and the value type. So, if you wanted a map of string to int, i.e. A unique identifier of type string referencing a value of type int, then the most readable way is to do it like this:
1
2
3
4
5
6
7
8
9
#include <map>
#include <string>

// ...

std::map<std::string, int> h;
h["key"] = 5;
h["other"] = 18;
h["HF"] = 140;
thanks!
Or you can use the std::make_pair(...) helper function with insert

1
2
3
4
5
6
7
8
9
#include <map>
#include <string>

// ...

std::map<std::string, int> h;
h.insert(std::make_pair("key",5));
h.insert(std::make_pair("other",18));
h.insert(std::make_pair("HF",140));
Topic archived. No new replies allowed.