user6470814 user6470814 - 1 year ago 122
C++ Question

Build Map With For Loop

I am trying to add values to a map variable that takes int keys and char values. The map is to contain the positions of letters in the alphabet, and the corresponding letter at that position. For some reason, I am getting an error from the

portion inside the for loop.

map<int, char> cipher;

for (int i = 0; i < 26; i++)
cipher.insert(i, char(97 + i));

Answer Source

Here is the correct syntax when using a map:

for (int i = 0; i < 26; i++)
    cipher[i] = char(97 + i);

//To use it
std::cout << cipher[letterindex] << std::endl;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download