gpt4 book ai didi

c++ - std::vector 的前地址是否移动不变?

转载 作者:IT老高 更新时间:2023-10-28 22:03:23 28 4
gpt4 key购买 nike

在以下代码段中:

std::vector<double> a(100, 4.2);
auto* a_ptr = a.data();

auto b = std::move(a);
auto* b_ptr = b.data();

std::cout << ((b_ptr == a_ptr) ? "TRUE" : "FALSE") << '\n';

C++ 标准是否保证 b_ptrstd::move 之后总是等于 a_ptr?在 wandbox 上运行代码打印 TRUE

最佳答案

来自 cppreference.com :

After container move construction (overload (6)), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in *this. The current standard makes this guarantee via the blanket statement in §23.2.1[container.requirements.general]/12, and a more direct guarantee is under consideration via LWG 2321.

指向元素的指针不会失效,包括指向第一个元素的指针。

关于c++ - std::vector 的前地址是否移动不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526424/

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