vector push_back calling copy_constructor more than once?

后端 未结 5 1210
刺人心
刺人心 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:24

    You're correct in assuming the additional invocation of the copy constructor comes from internal restructuring of the vector.

    See this answer for more detail: https://stackoverflow.com/a/10368636/3708904

    Or this answer for the reason why copy construction is necessary: https://stackoverflow.com/a/11166959/3708904

提交回复
热议问题