-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-containers.cc
70 lines (56 loc) · 2.06 KB
/
11-containers.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// Program
// Container usage of reserve, size and capacity.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 11-containers 11-containers.cc
//
// Execution
// ./11-containers
//
#include <iostream>
#include <vector>
//
// Entry function
//
int main() {
// NOTE:
// - size() function is showing real count of the elements pushed to the container
// - reserve() changes container capacity not size
// - A number passed to ctor will change its capaticty not the size
{
std::cout << "--- Container default init --- \n";
std::vector<int> vec {};
std::cout << " Size: " << vec.size() << '\n'; // Output: 0
std::cout << " Capacity: " << vec.capacity() << '\n'; // Output: 0
std::cout << " --- push_back an element\n";
vec.push_back(10);
std::cout << " Size: " << vec.size() << '\n'; // Output: 1
std::cout << " Capacity: " << vec.capacity() << '\n'; // OUtput: 1
}
{
std::cout << "--- Container initialized with a size --- \n";
std::vector<int> vec (10);
std::cout << " Size: " << vec.size() << '\n'; // Output: 10
std::cout << " Capacity: " << vec.capacity() << '\n'; // Output: 10
std::cout << " --- push_back an element\n";
vec.push_back(10);
std::cout << " Size: " << vec.size() << '\n'; // Output: 11
std::cout << " Capacity: " << vec.capacity() << '\n'; // Output: 20
}
{
std::cout << "--- Container default init --- \n";
std::vector<int> vec {};
std::cout << " Size: " << vec.size() << '\n'; // Output: 0
std::cout << " Capacity: " << vec.capacity() << '\n'; // Output: 0
std::cout << " --- Call reserve\n";
vec.reserve(10);
std::cout << " Size: " << vec.size() << '\n'; // Output: 0
std::cout << " Capacity: " << vec.capacity() << '\n'; // Output: 10
std::cout << " --- push_back an element\n";
vec.push_back(10);
std::cout << "Size: " << vec.size() << '\n'; // Output: 1
std::cout << "Capacity: " << vec.capacity() << '\n'; // Output: 10
}
return 0;
}