cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : string : string::capacity
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· string::c_str
· string::data
· string::empty
· string::end
· string::erase
· string::find
· string::find_first_not_of
· string::find_first_of
· string::find_last_not_of
· string::find_last_of
· string::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

string::capacity public member function
size_t capacity ( ) const;

Return size of allocated storage

Returns the size of the allocated storage space in the string object.

Notice that the capacity is not necessarily equal to the number of characters that conform the content of the string (this can be obtained with members size or length), but the capacity of the allocated space, which is either equal or greater than this content size.

Notice also that this capacity does not suppose a limit to the length of the string. If more space is required to accomodate content in the string object, the capacity is automatically expanded, or can even be explicitly modified by calling member reserve.

The real limit on the size a string object can reach is returned by member max_size.

Parameters

none

Return Value

The size of the currently allocated storage space in the string object.

size_t is an unsigned integral type.

Example

// comparing size, length, capacity and max_size
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Test string");
  cout << "size: " << str.size() << "\n";
  cout << "length: " << str.length() << "\n";
  cout << "capacity: " << str.capacity() << "\n";
  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: 4294967291

Basic template member declaration

( basic_string<charT,traits,Allocator> )
typedef typename Allocator::size_type size_type;
size_type capacity ( ) const;

See also

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

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us