哈希映射元素的值未正确打印

发布于 2024-11-08 13:27:43 字数 1172 浏览 1 评论 0原文

我在哈希映射中插入了一对(键,值),但在尝试打印它时似乎没有值。

问题是我不知道为什么我无法打印以下代码中的第二个元素。

让我们看一些代码:

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-11-15 13:27:43

m_memberNameMap[name]if 条件下做什么?如果映射中不存在键name,则会使用该键创建一个槽,并且值等于空字符串。

也就是说,表达式 m_memberNameMap[name] 调用 operator[],它将等于 string_pair(name, "") 的项目插入到映射中如果键name不存在,

解决方案是使用find as:

if (m_memberNameMap.find(name) == m_memberNameMap.end())
{
    m_memberNameMap.insert(string_pair(name,name));
    //rest of the code...
}

代替insert函数,您还可以使用:

   m_memberNameMap[name] = name; //simpler, cute!

毕竟, 运算符[]如果键不存在,则创建一个新槽。

What does m_memberNameMap[name] do in the if condition? If the key name 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] invokes operator[] which inserts an item equal to string_pair(name, "") to the map if the key name doesn't exist,

The solution is to use find as:

if (m_memberNameMap.find(name) == m_memberNameMap.end())
{
    m_memberNameMap.insert(string_pair(name,name));
    //rest of the code...
}

Instead of insert function, you can also use :

   m_memberNameMap[name] = name; //simpler, cute!

After all, operator[] creates a new slot if the key doesn't exist.

咽泪装欢 2024-11-15 13:27:43

这里有几个误解。首先,在映射中使用operator[],实际上会插入一个带有name键的新(空)元素。该运算符的语义是,如果您访问地图内的某个元素,它就必须存在,因此如果它不存在,则会创建它。然后,insert 操作被定义为 not 来添加该元素(如果该元素已存在),所以这就是您的问题。

您应该首先使用查找:

if (m_memberNameMap.find(name) == m_memberNameMap.end()) // don't exist
{
   // rest of your code
}

There are several misconceptions here. First, using the operator[] in the map, will actually insert a new (empty) element with the key name. 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, the insert operation is defined to not to add the element if it already exists, so this is your problem.

You should be using find first:

if (m_memberNameMap.find(name) == m_memberNameMap.end()) // don't exist
{
   // rest of your code
}
雨后咖啡店 2024-11-15 13:27:43

或者,如果您想使用operator [],您可以保存其结果以确保只发生一次查找:

string & value = m_memberNameMap[name];
if (value.empty()) {
   value = name;
   // ...
}

Or if you want to use operator [], you can just save its result to ensure only one lookup occurs:

string & value = m_memberNameMap[name];
if (value.empty()) {
   value = name;
   // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文