C++ Standard Library C++ STL Library

C++ <string> - max_size() Function



The C++ string::max_size function returns the maximum length the string can reach. The function returns the maximum potential length the string can reach due to known system or library implementation limitations.

Syntax

size_type max_size() const;
size_type max_size() const noexcept;

Parameters

No parameter is required.

Return Value

Maximum number of characters that can be held in a string.

Time Complexity

Constant i.e, Θ(1)

Example:

In the example below, the string::max_size function is used find out the maximum number of characters that a string can hold.

#include <iostream>
#include <string>

using namespace std;
 
int main (){
  string str = "Hello World!.";

  cout<<"Size of the String: "<<str.size()<<"\n";
  cout<<"Length of the String: "<<str.length()<<"\n";
  cout<<"Capacity of the String: "<<str.capacity()<<"\n";
  cout<<"Maximum size of the String: "<<str.max_size()<<"\n"; 
  return 0;
}

A possible output could be:

Size of the String: 13
Length of the String: 13
Capacity of the String: 15
Maximum size of the String: 9223372036854775807

❮ C++ <string> Library