public member function
<string>

std::basic_string::length

size_type length() const;
size_type length() const noexcept;
返回字串的長度
返回字串的長度,以字元數為單位。

這是構成 basic_string 內容的實際字元數,不一定等於其儲存容量

basic_string::sizebasic_string::length都是同義詞並返回相同的值。

引數



返回值

字串中的字元數。

成員型別size_type是一種無符號整型型別。

示例

1
2
3
4
5
6
7
8
9
10
// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " characters.\n";
  return 0;
}

輸出
The size of str is 11 characters


複雜度

未指定。
常量。

迭代器有效性

沒有變化。

資料競爭

該物件被訪問。

異常安全

無異常保證:此成員函式從不丟擲異常。

另見