哈希映射元素的值未正确打印
我在哈希映射中插入了一对(键,值),但在尝试打印它时似乎没有值。
问题是我不知道为什么我无法打印以下代码中的第二个元素。
让我们看一些代码:
.h
typedef hash_map<const string, string, strptrhash, strptrequal> StringHashMap;
StringHashMap m_memberNameMap;
typedef pair <const string,string> string_pair;
.cpp
void Powerdomain::addMemberName(const string& name){
if (m_memberNameMap[name]==""){
m_memberNameMap.insert(string_pair(name,name));
StringHashMap::iterator it = m_memberNameMap.begin();
cout << it->first << endl;
cout << it->second << endl;
cout << m_memberNameMap["MD1"] << endl;
}
with name="MD1" 此输出:
MD1
*blank*
*blank*
编辑
有关艾伦的答案:
ModuleType * moduletype4=new ModuleType("type4");
ModuleType * value = moduleTypeMap["type4"];
if (value==NULL) {
ModuleType& value2=*moduleTypeMap["type4"];
value2=*moduletype4;
cout << "correctly inserted" << endl;
}
cout << moduleTypeMap["type4"]->getName() << endl;
这不起作用。我现在可能很困惑!
I insert a pair (key,value) to my hashmap but it seems like it doesn't have the value when trying to print it
The thing is i don't know why i can't print the second element in the following code.
Let's see some code :
.h
typedef hash_map<const string, string, strptrhash, strptrequal> StringHashMap;
StringHashMap m_memberNameMap;
typedef pair <const string,string> string_pair;
.cpp
void Powerdomain::addMemberName(const string& name){
if (m_memberNameMap[name]==""){
m_memberNameMap.insert(string_pair(name,name));
StringHashMap::iterator it = m_memberNameMap.begin();
cout << it->first << endl;
cout << it->second << endl;
cout << m_memberNameMap["MD1"] << endl;
}
with name="MD1" this outputs :
MD1
*blank*
*blank*
EDIT
concerning the answer of Alan :
ModuleType * moduletype4=new ModuleType("type4");
ModuleType * value = moduleTypeMap["type4"];
if (value==NULL) {
ModuleType& value2=*moduleTypeMap["type4"];
value2=*moduletype4;
cout << "correctly inserted" << endl;
}
cout << moduleTypeMap["type4"]->getName() << endl;
This doesn't work. I Might be confused now !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
m_memberNameMap[name]
在if
条件下做什么?如果映射中不存在键name
,则会使用该键创建一个新槽,并且值等于空字符串。也就是说,表达式
m_memberNameMap[name]
调用operator[]
,它将等于string_pair(name, "")
的项目插入到映射中如果键name
不存在,解决方案是使用
find
as:代替
insert
函数,您还可以使用:毕竟,
运算符[]
如果键不存在,则创建一个新槽。What does
m_memberNameMap[name]
do in theif
condition? If the keyname
doesn't exist in the map, it creates a new slot with that key, and value equal to empty string.That is, the expression
m_memberNameMap[name]
invokesoperator[]
which inserts an item equal tostring_pair(name, "")
to the map if the keyname
doesn't exist,The solution is to use
find
as:Instead of
insert
function, you can also use :After all,
operator[]
creates a new slot if the key doesn't exist.这里有几个误解。首先,在映射中使用
operator[]
,实际上会插入一个带有name
键的新(空)元素。该运算符的语义是,如果您访问地图内的某个元素,它就必须存在,因此如果它不存在,则会创建它。然后,insert
操作被定义为 not 来添加该元素(如果该元素已存在),所以这就是您的问题。您应该首先使用查找:
There are several misconceptions here. First, using the
operator[]
in the map, will actually insert a new (empty) element with the keyname
. The semantics of this operator are that if you access an element inside the map, it has to be there, so if it doesn't exist, it is created. Then, theinsert
operation is defined to not to add the element if it already exists, so this is your problem.You should be using find first:
或者,如果您想使用
operator []
,您可以保存其结果以确保只发生一次查找:Or if you want to use
operator []
, you can just save its result to ensure only one lookup occurs: