GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::deque::shrink_to_fit(3) C++ Standard Libary std::deque::shrink_to_fit(3)

std::deque::shrink_to_fit - std::deque::shrink_to_fit


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


Requests the removal of unused capacity.


It is a non-binding request to reduce the memory usage without changing the size of
the sequence. It depends on the implementation whether the request is fulfilled.


All iterators and references are invalidated. Past-the-end iterator is also
invalidated.


(none)


-
T must meet the requirements of MoveInsertable.


(none)


At most linear in the size of the container.


If an exception is thrown other than by T's move constructor, there are no effects.

// Run this code


#include <iostream>
#include <new>
#include <deque>


// minimal C++11 allocator with debug output
template <class Tp>
struct NAlloc {
typedef Tp value_type;
NAlloc() = default;
template <class T> NAlloc(const NAlloc<T>&) {}
Tp* allocate(std::size_t n)
{
n *= sizeof(Tp);
std::cout << "allocating " << n << " bytes\n";
return static_cast<Tp*>(::operator new(n));
}
void deallocate(Tp* p, std::size_t n)
{
std::cout << "deallocating " << n*sizeof*p << " bytes\n";
::operator delete(p);
}
};
template <class T, class U>
bool operator==(const NAlloc<T>&, const NAlloc<U>&) { return true; }
template <class T, class U>
bool operator!=(const NAlloc<T>&, const NAlloc<U>&) { return false; }


int main() {
/* std::queue has no capacity() function (like std::vector),
* because of this we use a custom allocator to show
* the working of shrink_to_fit. */
std::cout << "Default-construct deque:\n";
std::deque<int, NAlloc<int>> deq;


std::cout << "\nAdd 300 elements:\n";
for (int i = 1000; i < 1300; ++i)
deq.push_back(i);


std::cout << "\nPop 100 elements:\n";
for (int i = 0; i < 100; ++i)
deq.pop_front();


std::cout << "\nRun shrink_to_fit:\n";
deq.shrink_to_fit();


std::cout << "\nDestroy deque as it goes out of scope:\n";
}


Default-construct deque:
allocating 64 bytes
allocating 512 bytes


Add 300 elements:
allocating 512 bytes
allocating 512 bytes


Pop 100 elements:


Run shrink_to_fit:
allocating 64 bytes
allocating 512 bytes
allocating 512 bytes
deallocating 512 bytes
deallocating 512 bytes
deallocating 512 bytes
deallocating 64 bytes


Destroy deque as it goes out of scope:
deallocating 512 bytes
deallocating 512 bytes
deallocating 64 bytes


size returns the number of elements
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.