std::char_traits::length

1
static std::size_t length( const char_type* s );

Returns the length of the character sequence pointed to by s, that is, the position of the terminating null character (CharT()).

Parameters

s - pointer to a character sequence to return length of

Return value

The length of character sequence pointed to by s.

Exceptions

(none).

Complexity

Linear.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
  
void print(const char* str)
{
  std::cout << "string '" << str << "' ";
  std::cout << "length = " << std::char_traits<char>::length(str) << '\n';
}
  
int main()
{
  print("foo");
  
  std::string s("booo");
  print(s.c_str());
}

Output:

1
2
string 'foo' length = 3
string 'booo' length = 4
doc_CPP
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.