The C++ capacity(), size(), erase(), length(), max_size() basic_string member functions program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the capacity() member function to return the largest number of elements that could be stored in a string without increasing the memory allocation of the string, size() member function to returns the current number of elements in a string, erase() member function to removes an element or a range of elements in a string from a specified position and length() member function to return the current number of elements in a string.

To show: How to use the C++ basic_string member functions: capacity(), size(), erase(), length() and max_size() in C++ programming

 

// the C++ capacity(), size(), erase(), length() and max_size() program example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

string str1("Testing the capacity()");

cout<<"str1 string is: "<<str1<<endl;

 

// the size and length member functions differ in name only

basic_string <char>::size_type SizeStr1, LenStr1;

SizeStr1 = str1.size();

LenStr1 = str1.length();

basic_string <char>::size_type CapStr1, MaxSizeStr1;

CapStr1 = str1.capacity();

MaxSizeStr1 = str1.max_size();

 

// compare size, length, capacity & max_size of a string

cout<<"\nOperation: str1.size()"<<endl;

cout<<"The size of str1 is: "<<SizeStr1<<" characters"<<endl;

cout<<"\nOperation: str1.length()"<<endl;

cout<<"The length of str1 is: "<<LenStr1<<" characters"<<endl;

cout<<"\nOperation: str1.capacity()"<<endl;

cout<<"The capacity of str1 is: "<<CapStr1<<" characters"<<endl;

cout<<"\nOperation: str1.max_size()"<<endl;

cout<<"The max_size of str1 is: "<<MaxSizeStr1<<" characters"<<endl;

 

// erase some characters

str1.erase(6, 5);

 

// re-check

cout<<"\nOperation: str1.erase(6, 5)"<<endl;

cout<<"The new str1 string is: "<<str1<<endl<<endl;

SizeStr1 = str1.size();

LenStr1 = str1.length();

CapStr1 = str1.capacity();

MaxSizeStr1 = str1.max_size();

 

// compare size, length, capacity & max_size of a string after erasing part of the original string

cout<<"The new size of str1 is: "<<SizeStr1<<" characters"<<endl;

cout<<"The new length of str1 is: "<<LenStr1<<" characters"<<endl;

cout<<"The new capacity of str1 is: "<<CapStr1<<" characters"<<endl;

cout<<"The new max_size of str1 is: "<<MaxSizeStr1<<" characters"<<endl;

 

return 0;

}

 

Output example:

 

str1 string is: Testing the capacity()

 

Operation: str1.size()

The size of str1 is: 22 characters

 

Operation: str1.length()

The length of str1 is: 22 characters

 

Operation: str1.capacity()

The capacity of str1 is: 31 characters

 

Operation: str1.max_size()

The max_size of str1 is: 4294967294 characters

 

Operation: str1.erase(6, 5)

The new str1 string is: Testin capacity()

 

The new size of str1 is: 17 characters

The new length of str1 is: 17 characters

The new capacity of str1 is: 31 characters

The new max_size of str1 is: 4294967294 characters

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index