A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_string_shrink_to_fit.htm below:

C++ String shrink_to_fit

C++ String Library - shrink_to_fit Description

It requests the string to reduce its capacity to fit its size.

Declaration

Following is the declaration for std::string::shrink_to_fit.

void shrink_to_fit();
C++11
void shrink_to_fit();
Parameters

none

Return Value

none

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::shrink_to_fit.

#include <iostream>
#include <string>

int main () {
   std::string str (500,'x');
   std::cout << "1. capacity of str: " << str.capacity() << '\n';

   str.resize(10);
   std::cout << "2. capacity of str: " << str.capacity() << '\n';

   str.shrink_to_fit();
   std::cout << "3. capacity of str: " << str.capacity() << '\n';

   return 0;
}

The sample output should be like this −

1. capacity of str: 500
2. capacity of str: 500
3. capacity of str: 10

string.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4