C++ new if statement with initializer
Yes, this is a typo. iterator
for std::map
will be dereferenced as std::map::value_type
, where value_type
is std::pair<const Key, T>
.
See example of usage for std::map::find
(from cppreference):
#include <iostream>
#include <map>
int main()
{
std::map<int,char> example = {{1,'a'},{2,'b'}};
auto search = example.find(2);
if (search != example.end()) {
std::cout << "Found " << search->first << " " << search->second << '\n';
} else {
std::cout << "Not found\n";
}
}
You are correct. The code as it is given does not compile. See here. The compiler error is:
error: 'struct std::pair<const int, std::__cxx11::basic_string<char> >' has no member named 'size'
std::pair
does not have a size
member. But std::string
has it.
So the correct code should be:
if (auto it = m.find(10); it != m.end()) { return it->second.size(); }