我见过类似以下的示例代码:
std::string s = "Hello World!";
std::map<char, std::size_t> h;
for (std::string::const_iterator i=s.cbegin(); i!=s.cend(); ++i)
{
++h[*i];
}
assert(h['l'] == 3);
Run Code Online (Sandbox Code Playgroud)
这似乎依赖于在每个字母的第一次出现时将值类型归零.即使使用std::size_t没有默认构造函数将其重置为零的东西,这是否有保证?