string reserve() in C++

reserve(): This function attempt to preallocate enough memory for specified number of

characters. 

Parameters: One parameter is required for this function.

__res_arg – number of characters required. 

This function is available in the below file.

File: basic_string.tcc

Syntax:

str.reserve(__res_arg)

For Example:

str = "abc"

str.reserve(10) => It reserve memory for 10 characters in the string.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "abc";

    str.reserve(10);

    cout << str.size() << "\n";

    return 0;
}


No comments:

Post a Comment