简体   繁体   中英

function to find length of 2d string array in cpp?

Is there any inbuilt function in cpp that can give me the length of a 2d char array

for ex, the function for args

const char* args[] = {"412", "..7", ".58", "7.8", "32.", "6..", "351", "3.9", "985", "...", ".46"}

should return 11

I like to use a template function for this.

template <typename T, std::size_t N>
std::size_t size(T(&)[N]) { return N; }

The advantage over an approach using sizeof is that you can't accidentally do it with a pointer.

这将给出args中元素的数量:

sizeof(args) / sizeof(args[0])

在C ++ 11中,您可以使用:

std::end(args) - std::begin(args)

sizeof(args) / sizeof(*args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM