Vector of Vector 如何在内存中对齐? [英] How is a Vector of Vector aligned in memory?

查看:129
本文介绍了Vector of Vector 如何在内存中对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解为

所以你的

ref.resize(i);

仅影响顶行.你的

ref[i].push_back(23);

只影响第 i 列.

I understand as Set size of vector of vectors at run time describes, one can declare vector of vector as

vector<vector<int> > ref;

then resize the first level by

ref.resize(i);

and push element at the 2nd level:

ref[i].push_back(23);

But how are vector of vector aligned in memory?

For simple vector, it's a container and align its element continuously, like an array; but in the case of vector of vector, I couldn't see the picture.

As the size of each inner vector (the vector in vector of vector) size might change, does the outer vector of vector (the vector in vector of vector) align inner vectors continously? Does the outer vector researve memeory space for each inner vector? what if one vector overshoot?

解决方案

The size of the vector<int> struct that is stored in ref is constant. Common implementations has this as three pointers, or around 12 bytes on 32-bit architectures, or 24 bytes on shiny new 64-bit architectures.

So ref manages roughly ref.capacity() * 12 bytes of continuous storage.

Each element/vector<int> in ref manages its own integers independent of the elements ref manages. In the artistic rendering below ref.size() == ref.capacity() for the sake of simplicity.

So your

ref.resize(i);

only affects the top row. Your

ref[i].push_back(23);

only affects the i-th column.

这篇关于Vector of Vector 如何在内存中对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆