• Advertisement
Sign in to follow this  

indexing maps and strings(C++)

This topic is 3114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okay, so i have an input file of two lines which i stored in 2 strings, 1 for each line. i have to multiply the letters of the string, where a = 1...z = 26, so i have a map<char, int> with all the pairs already initialized. the strings and map are all good, i output the strings and they're right and the map works fine with regular chars... yet when i index the map with chars that i in turn indexed from the string, i always get 0. so if i have a string like ABABA i expect 1*2*1*2*1 yet i get 0*0*0*0*0 the only reason i can think for this is that it didn't recognize the key i used and instead value-initialized a new int...why?

Share this post


Link to post
Share on other sites
Advertisement
In short, it's because 'a' != 'A'.

Why are you using a map<char, int>? Using regular math, 'a' - 'a' + 1 == 1 and 'z' - 'a' + 1 == 26

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement