isIsogram() function and std::map insert

Screenshot (14)

I was confused as to how we could insert an element into a map without any sort of insert or add function, so I looked it up. I learned a few things about map in the process. Thanks.

This is a great C++ refresher course, by the way. Definitely not just for people who want to learn it from scratch.

On line 46, looks like you used = when it should have been == to check for equality. Also, you actually don’t need the insert method – a nice feature of the brackets operator on a map is that if the entry you look up doesn’t exist, it will insert it for you. This means that just evaluating letterSeen[letter] will create the entry, all you have to do is set letterSeen[letter] = true if it isn’t already.

yeah, I caught that while running it just now. I could swear I tested this before I posted it. Another curiosity I’m running into is that I can’t get .insert to work with list initialization and a variable though documentation clearly shows this is possible. Also, for some reason even if I hard code the insert( like letterSeen.insert({‘a’, true}):wink: it doesnt falsify if the first element is the hard-coded letter. Scratching my head atm, but I’m sure I’m missing something simple.

and == true didnt work either. I could only get it to function with if(letterSeen[letter]). any way, the jist of the difference between insert and the default constructor method is that insert wont replace the value if the element is extant. Irrelevant here, of course. Also, insert is faster since it doesnt have to go through the default constructor. Again, not really relevant here, but I wanted to play with it to get a better understanding. Lastly, the default constructor is definitely more readable, but insert with list initialization is pretty good as well, so given that, it seemed like an overall better way to do it even it isn’t necessarily beneficial to the program at hand.

Privacy & Terms