Not using reserve results in quite some extra work.
struct Foo
{
static int i ;
int f;
Foo():f(i++){ cout << "in ctor:" << i << endl; }
Foo(const Foo&):f(i++){ cout << "in copy ctor:" << i << endl; }
};
int Foo::i = 1;
void tst_vec()
{
vector v;
v.reserve(10);
Foo f ;
v.push_back(f);
v.push_back(Foo());
v.push_back(Foo());
cout << "---" << endl;
Foo& temp = v[0];
}
---
with reserve:
in ctor:2
in copy ctor:3
in ctor:4
in copy ctor:5
in ctor:6
in copy ctor:7
---
without reserve:
in ctor:2
in copy ctor:3
in ctor:4
in copy ctor:5
in copy ctor:6
in ctor:7
in copy ctor:8
in copy ctor:9
in copy ctor:10
No comments:
Post a Comment