C++ Deque Library - shrink() Function


Advertisements

Description

The C++ function std::deque::shrink() requests the deque to reduce its capacity to fit its size.

Declaration

Following is the declaration for std::deque::shrink() function form std::deque header.

C++11

void shrink_to_fit();

Parameters

None

Return value

None

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::deque::shrink() function.

#include <iostream>
#include <deque>

using namespace std;

int main(void) {

   deque<int> d(10);

   cout << "Initial size of deque = " << d.size() << endl;

   d.resize(5);

   cout << "size of deque after resize operation = " << d.size() << endl;

   d.shrink_to_fit();

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Initial size of deque = 10
size of deque after resize operation = 5
deque.htm
Advertisements