C++ Array vs vector

前端 未结 8 1062
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 06:06

when using C++ vector, time spent is 718 milliseconds, while when I use Array, time is almost 0 milliseconds.

Why so much performance difference?

int         


        
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 06:53

    When you declare the array, it lives in the stack (or in static memory zone), which it's very fast, but can't increase its size.

    When you declare the vector, it assign dynamic memory, which it's not so fast, but is more flexible in the memory allocation, so you can change the size and not dimension it to the maximum size.

提交回复
热议问题