Finding the length of a fixed size array of any type is easy enough with a helper function template:
#include <cstddef> // for std::size_t
template< class T, size_t N >
std::size_t length(const T (&)[N] )
{
return N;
};
string s[]={"a","b","c"};
std::cout << length(s) << std::endl;
In C++11, the function would be constexpr
.
Concerning strlen, it counts chars
until it finds a null termination character \0
. When you call std::string'sc_str()
method, you get a pointer to the first char
in a null terminated string.