-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShrinkToFit.cpp
54 lines (44 loc) · 1.87 KB
/
ShrinkToFit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* \file ShrinkToFit.cpp
* \brief Minimize the capacity of a container just enough to hold existing range.
*
* Standard library containers often allocate more memory than the actual number of elements in them.
* Such a policy results in an optimization of saving some allocations when a container grows in size.
* On the other hand, when size of the container reduces, there is often leftover capacity
* in the container. The leftover capacity of the container can be unnecessary wastage of memory
* resources. Shrink-to-fit idiom has been developed to reduce the extra capacity to a minimum
* required capacity and thereby saving memory resources.
*
* In C++11 some containers declare such idiom as function shrink_to_fit(),
* e.g. vector, deque, basic_string. shrink_to_fit() is a non-binding request
* to reduce capacity() to size()
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
int main(int, char **)
{
{
std::vector<int> v;
v.reserve(64);
std::cout << STD_TRACE_VAR2(v.size(), v.capacity()) << " -> ";
// v is swapped with its temporary copy, which is capacity optimal
std::vector<int>(v).swap(v);
std::cout << STD_TRACE_VAR2(v.size(), v.capacity()) << std::endl;
}
{
std::vector<int> v;
v.reserve(64);
std::cout << STD_TRACE_VAR2(v.size(), v.capacity()) << " -> ";
// v is swapped with its temporary copy, which is capacity optimal
std::vector<int>(v.begin(), v.end()).swap(v);
std::cout << STD_TRACE_VAR2(v.size(), v.capacity()) << std::endl;
}
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
v.size(): 0, v.capacity(): 64 -> v.size(): 0, v.capacity(): 0
v.size(): 0, v.capacity(): 64 -> v.size(): 0, v.capacity(): 0
#endif