How to reliably get size of C-style array?

后端 未结 10 1980
广开言路
广开言路 2020-11-29 09:37

How do I reliably get the size of a C-style array? The method often recommended seems to be to use sizeof, but it doesn\'t work in the foo function

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 10:19

    Since c++11, there is a very convenient way:

    static const int array[] = { 1, 2, 3, 6 };
    int size = (int)std::distance(std::begin(array), std::end(array))+1;
    

提交回复
热议问题