A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/string/capacity/ below:

public member function

<string>

std::string::capacity
size_t capacity() const noexcept;

Return size of allocated storage

Returns the size of the storage space currently allocated for the string, expressed in terms of bytes.

This capacity is not necessarily equal to the string length. It can be equal or greater, with the extra space allowing the object to optimize its operations when new characters are added to the string.

Notice that this capacity does not suppose a limit on the length of the string. When this capacity is exhausted and more is needed, it is automatically expanded by the object (reallocating it storage space). The theoretical limit on the length of a string is given by member max_size.

The capacity of a string can be altered any time the object is modified, even if this modification implies a reduction in size or if the capacity has not been exhausted (this is in contrast with the guarantees given to capacity in vector containers).

The capacity of a string can be explicitly altered by calling member reserve.



Parameters none

Return Value The size of the storage capacity currently allocated for the string.

size_t is an unsigned integral type (the same as member type string::size_type).



Example
1
2
3
4
5
6
7
8
9
10
11
12
13
// comparing size, length, capacity and max_size
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "length: " << str.length() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  std::cout << "max_size: " << str.max_size() << "\n";
  return 0;
}

A possible output for this program could be:
size: 11
length: 11
capacity: 15
max_size: 429496729


Complexity Unspecified, but generally constant.

Iterator validity No changes.

Data races The object is accessed.

Exception safetyNo-throw guarantee: this member function never throws exceptions.

See also
string::reserve
Request a change in capacity (public member function)
string::length
Return length of string (public member function)
string::size
Return length of string (public member function)
string::max_size
Return maximum size of string (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4