C++ String Library - capacity


Advertisements


Description

It returns the size of the storage space currently allocated for the string, expressed in terms of byte.

Declaration

Following is the declaration for std::string::capacity.

size_t capacity() const;

C++11

size_t capacity() const noexcept;

Parameters

none

Return Value

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

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::capacity.

Live Demo
#include <iostream>
#include <string>

int main () {
   std::string str ("Sairamkrishna Mammahe");
   std::cout << "capacity: " << str.capacity() << "\n";
   return 0;
}

The sample output should be like this −

capacity: 21

string.htm

Advertisements