Iterator to map::end?

Hi all,

I'm working with a map that essentially maps a letter to a tally, for example {"a":4, "b":6}. I'm trying to make a function like increment_count, which accepts a letter and increments the corresponding count, if it exists, or adds it to the mapping with a value of 1. For instance, increment_count("a") should then make the mapping {"a":5, "b":6}, and then increment_count("c") should make {"a":5, "b":6, "c":1}.

The method I have in mind sets the iterator to whatever map::find results in, but I have no idea how to write a conditional statement that can check whether the iterator is pointing to something that isn't map::end. Also, if there is a cleaner or more efficient way to do what I've outlined, please let me know! I'm a complete C++ noob, so anything I can learn would be awesome.

Thanks so much!
1
2
3
4
if(MyMap.find("My Key") != MyMap.end())
{
  //Yay we found "My Key" in the map :D
}
Last edited on
Topic archived. No new replies allowed.