C++ counting instances / histogram using std::map

前端 未结 2 938
庸人自扰
庸人自扰 2020-12-21 03:52

I have seen sample code similar to the following:

std::string s = \"Hello World!\";
std::map h;

for (std::string::const_iterator i=         


        
相关标签:
2条回答
  • 2020-12-21 04:10

    Indeed that's how map works: The []-operator is mutating and will create the object of mapped type if it does not exist yet. Since size_t value-initializes to zero, you're all fine.

    0 讨论(0)
  • 2020-12-21 04:20

    Quoting MSDN:

    POD and scalar types will always be zero initialized if instantiated with the default constructor syntax.

    So, assuming that map creates new entries at missing keys using a default constructor then yes, size_t will be initialised to zero.

    0 讨论(0)
提交回复
热议问题