vector push_back calling copy_constructor more than once?

后端 未结 5 1206
刺人心
刺人心 2020-11-29 10:23

I am a bit confused with the way vector push_back behaves, with the following snippet I expected the copy constructor to be invoked only twice, but the output suggest otherw

5条回答
  •  借酒劲吻你
    2020-11-29 11:18

    When the size of the vector is increased with the second push_back, the existing contents of the vector must be copied to a new buffer. To verify, output myints.capacity() after the first push_back, it should be 1.

提交回复
热议问题