Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There's a strlen, and a wcslen, but is there a template function like strlen<char> or strlen<wchar_t>?

Tags:

c++

There's a strlen, and a wcslen function, but is there a templated character array length function so you can do something like strlen<char> or strlen<wchar_t>?

If not, then I guess I'll write my own.

like image 778
leetNightshade Avatar asked Mar 31 '11 17:03

leetNightshade


People also ask

What can I use instead of strlen?

strlen() in C-style strings can be replaced by C++ std::strings. sizeof() in C is as an argument to functions like malloc(), memcpy() or memset() can be replaced by C++ (use new, std::copy(), and std::fill() or constructors).

Can we use strlen with string?

The strlen() function in C is used to calculate the length of a string. Note: strlen() calculates the length of a string up to, but not including, the terminating null character. Return Value: The function returns the length of the string passed to it.

What is Strnlen in C?

The strnlen() function computes the length of the string pointed to by s , not including the terminating null character, up to a maximum of maxlen bytes. The function doesn't check any more than the first maxlen bytes.


1 Answers

You have the char_traits helper used by std::string.

It provides char_traits<char>::length and char_traits<wchar_t>::length.

like image 106
Bo Persson Avatar answered Sep 30 '22 13:09

Bo Persson