stuck. Can anyone please help me!
Problem: I need to count occurence of strings in a data strcture and I created
std::map<std::string, int> ds;
Initially empty and input strings will be provided from stdin.
I tried
auto it = ds.find("someString")
to find the string but I am not sure about what if string is not present in the
ds
(what it will be it will be NULL?) and how to increase its count if present?
at max C++14 not C++17
PS: if you can suggest me some good resource to read for maps that will also be helpful. 😊
ds["somestring"]++ worked for me if the entry exists, it will get incremented, and if it does not, it will get default-initialized (which happens to initialize with zero) and then incremented if (it != std::end(ds)) {/* your code if string is found here */} To test if an iterator is returned from a find() method
cppreference.com
Обсуждают сегодня