gpt4 book ai didi

c++ - 将 deque 转换到 void 指针和从 void 指针安全吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:10 24 4
gpt4 key购买 nike

文档说

unlike vectors, deques are not guaranteed to store all its elements in contiguous storage locations

这是否意味着如果我将双端队列转换为 void * 然后返回到原始双端队列,我最终可能会在其中得到一些随机数据?

最佳答案

不,这意味着您不能获取双端队列的第一个元素的地址,将其转换为指针,然后在假设其余元素连续排列的情况下执行指针算术:

deque<int> d;
//...
int* x = &(d[0]);
int secondElement = x[1]; // illegal

vector<int> v;
//...
int* x = &(v[0]);
int secondElement = x[1]; // legal

您可以将双端队列的 address 转换为 void* 并毫无顾虑地将其转换回去。

关于c++ - 将 deque 转换到 void 指针和从 void 指针安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23649939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com