Could any ppl explain the output from vector code below?
R*r
3 楼
猎头发来的。我没有和这个猎头打过交道。贴在这里攒点RP,呵呵。
X*r
4 楼
Why don't you also track the constructor and copy constructor? That would answer your question.
s*u
5 楼
I believe the employer should be HDR
n*d
6 楼
Here it is. Why is it copied twice for each push? And the order looks strange? Circle Circle Circle push Circle(&c) Circle(&c) Circle(&c) ~Circle Circle(&c) Circle(&c) Circle(&c) ~Circle ~Circle iterator Circle::draw Circle::draw Circle::draw done ~Circle ~Circle ~Circle ~Circle ~Circle ~Circle
【在 X****r 的大作中提到】 : Why don't you also track the constructor and copy constructor? That would : answer your question.
X*r
7 楼
No, it is not exactly copied twice for each push. Some push are more costly than others. More hints: 1. Add tracking statements between the pushes, so you know which push causes what. 2. Print 'this' in the constructor and both 'this' and the address of the object being copied from in the copy constructor, so you know which object is copied to where. After you have done these, 3. Try adding 'circles.reserve(3);' before the first push_back, see if that changes anything, and think about why. Lear
【在 n**d 的大作中提到】 : Here it is. Why is it copied twice for each push? And the order looks : strange? : Circle : Circle : Circle : push : Circle(&c) : Circle(&c) : Circle(&c) : ~Circle
n*d
8 楼
I may know the answer. vector has to resize the memory for each push and then move the existing data to new location.
costly causes that
【在 X****r 的大作中提到】 : No, it is not exactly copied twice for each push. Some push are more costly : than others. : More hints: : 1. Add tracking statements between the pushes, so you know which push causes : what. : 2. Print 'this' in the constructor and both 'this' and the address of the : object being copied from in the copy constructor, so you know which object : is copied to where. : After you have done these, : 3. Try adding 'circles.reserve(3);' before the first push_back, see if that
n*d
9 楼
That is. Here is the result. Circle, this: 0012FF60 Circle, this: 0012FF5C Circle, this: 0012FF58 push C1 Circle 003707E0 (&c:0012FF60) push C2 Circle 00370818 (&c:003707E0) Circle 00370819 (&c:0012FF5C) ~Circle push C3 Circle 003707E0 (&c:00370818) Circle 003707E1 (&c:00370819) Circle 003707E2 (&c:0012FF58) ~Circle ~Circle iterator Circle::draw Circle::draw Circle::draw done ~Circle ~Circle ~Circle ~Circle ~Circle ~Circle
【在 n**d 的大作中提到】 : I may know the answer. vector has to resize the memory for each push and : then move the existing data to new location. : : costly : causes : that
when your obj was delivered as an argument without using reference type, the copy construtor would be invoked to duplicate a object. it is costly sometimes