The C++ basic_string class member, resize() and size() 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 resize() to specify a new size for a string, appending or erasing elements as required and size() to return the current number of elements in a string in C++ programming
To show: How to use the resize() and size() functions of the basic_string class member in C++ programming
// the resize() and size() C++ example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
string str1("Testing the resize()");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type SizeStr1;
SizeStr1 = str1.size();
basic_string <char>::size_type CapaStr1;
CapaStr1 = str1.capacity();
// compare size & capacity of the original string
cout<<"The size of str1 string is: "<<SizeStr1<<endl;
cout<<"The capacity of str1 string is: "<<CapaStr1<<endl;
// use resize() to increase size by 3 elements of the question mark
cout<<"\nOperation: str1.resize(str1.size() + 3, '?')"<<endl;
str1.resize(str1.size() + 3, '?');
cout<<"The resized str1 string is: "<<str1<<endl;
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
// compare size & capacity of a string after resizing
cout<<"The size of resized str1 string is: "<<SizeStr1<<endl;
cout<<"The capacity of resized str1 string is: "<<CapaStr1<<endl;
// use resize() to increase size by 10 elements:
cout<<"\nOperation: str1.resize(str1.size() + 10)"<<endl;
str1.resize(str1.size() + 10);
cout<<"The resized str1 string is: "<<str1<<endl;
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
// compare size & capacity of a string after resizing, capacity increases automatically as required
cout<<"The increased size of str1 string is: "<<SizeStr1<<endl;
cout<<"The increased capacity of str1 string is: "<<CapaStr1<<endl;
// use resize() to downsize by 20 elements:
cout<<"\nOperation: str1.resize(str1.size() - 20)"<<endl;
str1.resize(str1.size() - 20);
cout<<"The downsized str1 string is: "<<str1<<endl;
SizeStr1 = str1.size();
CapaStr1 = str1.capacity();
// compare size & capacity of a string after downsizing
cout<<"The size of downsized str1 string is: "<<SizeStr1<<endl;
cout<<"The capacity of downsized str1 string is: "<<CapaStr1<<endl;
return 0;
}
Output example:
str1 string is: Testing the resize()
The size of str1 string is: 20
The capacity of str1 string is: 31
Operation: str1.resize(str1.size() + 3, '?')
The resized str1 string is: Testing the resize()???
The size of resized str1 string is: 23
The capacity of resized str1 string is: 31
Operation: str1.resize(str1.size() + 10)
The resized str1 string is: Testing the resize()???
The increased size of str1 string is: 33
The increased capacity of str1 string is: 47
Operation: str1.resize(str1.size() - 20)
The downsized str1 string is: Testing the r
The size of downsized str1 string is: 13
The capacity of downsized str1 string is: 47
Press any key to continue . . .