public member function
<string>

std::string::size

size_t size() const;
size_t size() const noexcept;
Return length of string
Returns the length of the string, in terms of number of characters.

This is the number of actual characters that conform the contents of the string, which is not necessarily equal to its storage capacity.

Both string::size and string::length are synonyms and return the same value.

Parameters

none

Return Value

The number of characters in the string.

size_t is an unsigned integral type.

Example

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

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


Output:
The size of str is 11 characters

Complexity

Unspecified.
Constant.

Iterator validity

No changes.

Data races

The object is accessed.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also