如果我有一个 std::vector<std::vector<char>>
其中以下为真:
- 内部向量的大小从不更改
- 外部向量的大小通过插入和删除发生变化
我是否可以像这样将地址传递到内部矢量中,在修改外部矢量后使用并且是安全的?
std::vector<std::vector<char>> buffer;
#include <code that inserts into "buffer" uhhh 10 vectors with 100 char each>
char * ch = buffer[5].data();
// example of code that never erases/resizes the vector "ch" is pointing into...
// ... BUT will remove/insert vectors around it, prompting "buffer" to reallocate
buffer.erase(buffer.begin(), buffer.begin() + 4);
buffer.shrink_to_fit();
buffer.insert(buffer.begin(), std::vector<char>(1));
assert(buffer[1].data() == ch); // the 5th inner-vector moved to index 1
printf("how is this bit%c?", *ch); // but hopefully "ch" is still valid?