【发布时间】:2010-10-05 16:27:24
【问题描述】:
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 将变得无效吗?
【问题讨论】:
-
简短回答:可能不,长回答:除非您以前使用
typedef const std::string string;而不是using namespace std;,否则它不会编译,这很令人困惑......