#include#include using namespace std; int main() { string s; int n; cout << "Enter the size of the string to be reserved: "; cin >> n; s.reserve(n); // Allocate memory for the string // Use the string ... s = "This is a reserved string."; cout << s << endl; return 0; }
#includeThis code copies one string into another by allocating enough memory for the second string using the reserve() function before copying the first string into it. Package library: Standard C++ Library In both examples, the package library used is the Standard C++ Library.#include using namespace std; int main() { string s1 = "This is a long string"; string s2; s2.reserve(s1.size()); // Allocate memory for the second string // Copy the first string into the second string for (int i = 0; i < s1.size(); i++) { s2 += s1[i]; } cout << s2 << endl; return 0; }