◐ Shell
clean mode source ↗

std::char_traits<CharT>::length - cppreference.com

提供: cppreference.com

<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>

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

(C++17未満)

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

(C++17以上)

s の指す文字シーケンスの長さ、つまり、終端のヌル文字 (CharT()) の位置を返します。

引数

戻り値

s の指す文字シーケンスの長さ。

例外

(なし)

計算量

線形。

#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());
}

出力:

string 'foo' length = 3
string 'booo' length = 4