gpt4 book ai didi

c++ - std::map 可以在调用 const 函数期间重新平衡吗?

转载 作者:可可西里 更新时间:2023-11-01 18:20:01 26 4
gpt4 key购买 nike

我有一个 const std::map<std::string, std::vector<double>>成员变量和函数 const std::vector* foo() .我希望此函数有时返回指向此 map 元素的指针。但我担心 map 可能会重新平衡 - 即使在 std::map 期间也是如此标记为 const 的功能- 使我返回的指针无效。我知道对 map 的任何后续修改都会使我的指针无效,但由于我已将成员变量标记为 const,所以这种情况不会发生。 .

我有时无法返回引用,foo需要返回nullptr .

我所做的安全吗?

最佳答案

标准很明确:唯一可以无效的映射的迭代器或指针或引用正在删除它指向的元素。您甚至可以插入其他元素不会使您的指针无效。见§23.2.4/9:

The insert and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.

关于c++ - std::map 可以在调用 const 函数期间重新平衡吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26867435/

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