W3cubDocs

/C++

std::char_traits<CharT>::length

static std::size_t length( const char_type* s );
(until C++17)
static constexpr std::size_t length( const char_type* s );
(since C++17)

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

Throws nothing.

Complexity

Linear.

Example

#include <iomanip>
#include <iostream>
#include <string>
 
void print(const char* str)
{
    std::cout << std::quoted(str) << " has length = "
              << std::char_traits<char>::length(str) << '\n';
}
 
int main()
{
    print("foo");
 
    std::string s{"booo"};
    print(s.c_str());
}

Output:

"foo" has length = 3
"booo" has length = 4

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/string/char_traits/length