String Reserve Function - C++ Programming Tutorial
C++ Course / Strings / String Reserve Function

String Reserve Function

BLUF: Mastering String Reserve Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Reserve Function

C++ is renowned for its efficiency. Learn how String Reserve Function enables low-level control and high-performance computing in the tutorial below.

C++ String reserve

This function requests a change in capacity.

Syntax

Consider a string labeled as str and let l denote the intended length of this string. The syntax for this scenario is:

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 instance, we can see how the size of the string changes when utilizing the reserve method.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience