String Reserve Function

C++ String reserve

This function requests a change in capacity.

Syntax

Consider a string str and l is planned length of the string. Its syntax would be:

Example

str.reserve(l);

Parameter

l is the planned length for the string

Note: capacity of the string can be greater than or equal to 'l'.

Return value

It does not return any value.

Example

Let's see the simple example.

Example

#include<iostream>
using namespace std;
int main()
{
	string s="Computer Science Technology";
	cout<<?Capacity is :?<<s.capacity()<<'\n';
            s.reserve(33);
            cout<<?Now,capacity is :?<<s.capacity();
	return 0;
}

Output:

Output

Capacity is 27
Now,capacity is : 54

In this example, we observe that how capacity of the string varies using reserve function.

Input Required

This code uses input(). Please provide values below: