Could someone explain me map<string,int>

Hello could someone explain me this particular codes as i dont understand them quite right:

1
2
  map<string,int> m = {{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},
                        {"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9}};


1
2
3
4
transform(transformedInput.begin(), transformedInput.end(), transformedInput.begin(), ::tolower);
        if (m.find(transformedInput) == m.end()) {
            cout << "Wrong entry!" << endl;
        } else break;


And for which do i need this librarys;
1
2
#include <cctype> 
#include <algorithm> 
Last edited on
map<string, int> is essentially an associative array (also called a dictionary).
it is essentially the opposite of vector<string>. so instead of indexing with ints ie:
myvec[0], myvec[1], myvec[2], ... it would be
mymap["a string"] = some_value

edit: you would need both, and <map>. also this looks like heavy stl stuff. User LB (his name on the forum is L B) would know more about this than me
Last edited on
A map is a key-value pair. The first item is the key, the second item is a value. It makes it very quick to look-up the value if you have the key.

In your case:
1
2
3
4
5
6
map<string,int> m = // if you have a string, it finds the integer value for you
{{ "zero", 0 },      // Your first element is a pair with string "zero" corresponding to int 0.
 { "one" , 1 },
 { "two" , 2 },
 ...
};

See : http://www.cplusplus.com/reference/map/map/

Next:
1
2
3
4
transform(transformedInput.begin(), 
          transformedInput.end(), 
          transformedInput.begin(), 
          ::tolower);

This will take every value between transformedInput.begin() to transformedInput.end(), perform the ::tolower operation on them, and write them back into transformedInput. You need to #include <algorithm> because this function is defined in <algorithm>
http://www.cplusplus.com/reference/algorithm/transform/
I'm guessing transformedInput is a string, it just makes each character lower-case.


Next:
1
2
3
if (m.find(transformedInput) == m.end()) {
  cout << "Wrong entry!" << endl;
} else break;

m.find() will search the map keys for something that matches transformedInput. If it is not found, it will return m.end(). Otherwise it will return a pointer (or iterator) to the matching key/value pair.
http://www.cplusplus.com/reference/map/map/find/

This might help:
1
2
3
4
5
6
7
8
map<string,int>::iterator it = m.find(transformedInput);
if (it == m.end()) {
  cout << "Wrong entry!" << endl;
} else 
{
  cout << "Associated \"" << transformedInput << "\" with " << it->second << endl;
  break;
}
Associated "two" with 2


<cctype> is needed for the ::tolower function. This is what is in there:
http://www.cplusplus.com/reference/cctype/
and this is the function:
http://www.cplusplus.com/reference/cctype/tolower/

Last edited on
Thank you guys you are the best. I have found all that on www.cplusplus.com site, but the thing is i don't quite understand some of the explanation. So it is more easy for me if somebody explains it with his own words.

Cheers, Z.
Topic archived. No new replies allowed.