Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5934171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:04:36+00:00 2026-05-22T15:04:36+00:00

I insert a pair (key,value) to my hashmap but it seems like it doesn’t

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:04:36+00:00Added an answer on May 22, 2026 at 3:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert an key/value pair into a serializeArray(from jquery). So I
I just need to be able to insert a key/value pair into an object
How would I insert a key => value pair into the midst of a
I'm trying to insert some value pairs into a std::map. In the first case,
I need an algorithm to store a key/value pair, where the key is an
I need to store strings in key value format. So am using Map like
I am trying to insert into an unordered_map with a std::set value declared as
I want to insert a pair< string, vector<float> > into a map, first it
Like: insert into table (col) values (N'multilingual unicode strings') I'm using SQL Server 2008
I am trying to INSERT INTO a table using the input from another table.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.