How do I find the length of an array?

前端 未结 27 2888
轮回少年
轮回少年 2020-11-21 23:10

Is there a way to find how many values an array has? Detecting whether or not I\'ve reached the end of an array would also work.

27条回答
  •  名媛妹妹
    2020-11-21 23:12

    While this is an old question, it's worth updating the answer to C++17. In the standard library there is now the templated function std::size(), which returns the number of elements in both a std container or a C-style array. For example:

    #include 
    
    uint32_t data[] = {10, 20, 30, 40};
    auto dataSize = std::size(data);
    // dataSize == 4
    

提交回复
热议问题