public member function
<string>

std::string::length

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

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

請注意,字串物件處理位元組時,不瞭解可能最終用於編碼其包含的字元的編碼。 因此,返回的值可能與多位元組或可變長度字元(例如 UTF-8)序列中編碼字元的實際數量不符。

string::sizestring::length都是同義詞,並返回完全相同的值。

引數



返回值

字串中的位元組數。

size_t 是一個無符號整數型別(與成員型別 string::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() << " bytes.\n";
  return 0;
}

輸出
The size of str is 11 bytes


複雜度

未指定。
常量。

迭代器有效性

沒有變化。

資料競爭

該物件被訪問。

異常安全

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

另見