vector的扩容机制使得它比较慢的原因

内存初始状态:

xxxxxxxxxx000000000000000000000000000000000000000000000000000000000000

vector申请内存:

xxxxxxxxxx~~~~~~~000000000000000000000000000000000000000000000000

vector后面的内存也被申请:

xxxxxxxxxx~~~~~~~xxxxxxxx00000000000000000000000000000000000000000

vector被存满,申请了新的内存,并把原来内存的内容复制到新申请的内存中:

xxxxxxxxxx~~~~~~~xxxxxxxx~~~~~~~~~~~~~~00000000000000000

释放原来的内存:

xxxxxxxxxx000000000000xxxxxxxx~~~~~~~~~~~~~~00000000000000000

vector扩容时会将原来的内容进行copy,这个操作的速度很慢

posted @ 2021-05-12 21:11  牟翔宇  阅读(195)  评论(0编辑  收藏  举报