Hello, I've been a lurker here for a little and am now compelled to join because I need help with a problem I've been working on.
For my project, I am trying to write a program that gets a string input from the user and add all the characters to a map where the key value is the letter and the mapped value is the frequency. If the letter already exists (is repeated in string) then I want the mapped value (frequency) to increment by one. I'm really confused on how to use a lot of the map class functions work exactly. Reading the C++ reference pages has confused me further. My thought process while writing the code was to use the find function to see if a key exists, if it doesn't exist, insert a new element with frequency 1 or increment an existing key's mapped-value by 1. I'm really new to C++ so any advice would be appreciated. This is where I am so far (this is an excerpt, the rest of my program just displays the information in a formatted list):
for(unsignedint i = 0; i < user_text.size(); ++i)
{
// search for the (character represented by the) number i in letters.
iter = letters.find(i);
if(iter == letters.end())
{
// the number wasn't found, so add it and make the count 1.
letters[i] = 1;
}
else
{
// the number was found, so increase it's count.
letters[i]++;
}
}
As you can see, you didn't refer to user_text anywhere in the body of your for loop.