public static member function
<string>

std::char_traits::length

static size_t length (const char_type* s);
Get length of null-terminated string
Returns the length of the null-terminated character sequence s.

The behavior implemented by all character traits types shall be to return the position of the first character for which member eq returns false when compared against charT().

This function is equivalent to strlen (for char) and wcslen (for wchar_t).

Parameters

s
Pointer to a null-terminated character sequence.
Member type char_type is the character type (i.e., the class template parameter in char_traits).

Return Value

Returns the length of s.

size_t is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
// char_traits::length
#include <iostream>   // std::cout
#include <string>     // std::char_traits

int main ()
{
  const char * foo = "String literal";
  std::cout << "foo has a length of ";
  std::cout << std::char_traits<char>::length(foo);
  std::cout << " characters.\n";
  return 0;
}


Output:
foo has a length of 14 characters.

Complexity

Linear in the returned value.

Exception safety

If s points a null-terminated character sequence, this member function never throws exceptions (no-throw guarantee) in any of the standard specializations.
Otherwise, it causes undefined behavior.

See also