gpt4 book ai didi

c++ - std::map,指向映射键值的指针,这可能吗?

转载 作者:IT老高 更新时间:2023-10-28 12:58:42 27 4
gpt4 key购买 nike

std::map<std::string, std::string> myMap;

std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string);
if(i == m_imagesMap.end())
return NULL;

string *p = &i->first;

最后一行有效吗?我想将此指针 p 存储在其他地方,它在整个程序生命周期内都有效吗?但是如果我在这个映射中添加更多元素(使用其他唯一键)或删除一些其他键会发生什么,它不会重新分配这个字符串(键值对),所以 p 将变得无效吗?

最佳答案

第 23.1.2#8 节(关联容器要求):

The insert 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.

所以 yes 保证存储指向 map 元素的数据成员的指针是有效的,除非您删除 那个 元素。

关于c++ - std::map,指向映射键值的指针,这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/516007/

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