std::basic_string<CharT,Traits,Allocator>::capacity (3) - Linux Manuals

std::basic_string<CharT,Traits,Allocator>::capacity: std::basic_string<CharT,Traits,Allocator>::capacity

NAME

std::basic_string<CharT,Traits,Allocator>::capacity - std::basic_string<CharT,Traits,Allocator>::capacity

Synopsis


size_type capacity() const; (until C++11)
size_type capacity() const noexcept; (since C++11)


Returns the number of characters that the string has currently allocated space for.

Parameters


(none)

Return value


Capacity of the currently allocated storage

Complexity


Constant

Example


// Run this code


  #include <iostream>
  #include <string>


  void show_capacity(std::string const& s)
  {
      std::cout << "'" << s << "' has capacity " << s.capacity() << ".\n";
  }


  int main()
  {
      std::string s{"Exemplar"};
      show_capacity(s);


      s += " is an example string.";
      show_capacity(s);
  }

Possible output:


  'Exemplar' has capacity 15.
  'Exemplar is an example string.' has capacity 30.

See also


        returns the number of characters
size (public member function)
length
        reserves storage
reserve (public member function)