C++ std -- vector
I got quite confused the first time I read vector implementation. After a few minutes’ struggle, I realized that there are two implementations: the general vector
and the specialized vector<boo>
. There are quite a few differences between them. For example, the ::clear()
function. The general implementation is here. The bool specialized version is here. You see the difference. The former call destructor. The latter does not.
std::vector<T, Allocator>::clear()
My first question about clear
function is whether it calls the destructor. For the general implementation, yes, it does.
Let’s analyze a simple program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct A {
int n;
A(int n):n(n){}
~A() {cout << "destroy " << n << endl;}
};
int main() {
vector<A> x;
cout << "-- delimiter 0 ---" << endl;
x.emplace_back(1);
cout << "-- delimiter 1 ---" << endl;
x.emplace_back(2);
cout << "-- delimiter 2 ---" << endl;
x.clear();
cout << "-- delimiter 3 ---" << endl;
}
The output is
1
2
3
4
5
6
7
8
$ g++ test.cc -std=c++17 && ./a.out
-- delimiter 0 ---
-- delimiter 1 ---
destroy 1
-- delimiter 2 ---
destroy 2
destroy 1
-- delimiter 3 ---
You see that After calling x.clear()
, both elements are destroyed.
Wait, why the first element is destroyed twice? This is because, when pushing the second the element, it goes beyond its internal capacity, so it needs to creates a new buffer and copy existing elements to the new buffer, and during this process, the old elements will be destroyed. If you add x.reserve(2);
at the beginning, then you won’t see it.