Adding data inside unordered_map inside a class

I have a class Citydata, defined in .hh file

struct City_details {
   string name;
   int taxrate;
};

class Citydata {
public:
   bool add_data(string id, string name, int taxrate)
   //other member functions...
private:
   unordered_map<id, City_details> info_map;

I have trouble implementing the add_data function in .cc file. Here is my try.

bool Citydata::add_data(string id, string name, int taxes) {
   if ( info_map.find(id) == info_map.end()) {
       City_details dataload;
       dataload.name = name;
       dataload.taxrate = taxes;
       info_map[id] = dataload;
       return true;
   }
   else return false;
}

When I test this, I cannot see any data added the way I wanted. Instead I see one completely empty entry (default empty values), and one entry with right id (the key) but no data added to the parameters. Each time I call add_data, it creates similar pair of one completely empty key-data member, then one with right id and otherwise empty data.

The original program is much longer than this, so problem might persist there too, but I wonder if my approach is flawed by design.

1 answer

  • answered 2021-11-25 06:00 Anoop Rana

    I have made some modifications that makes your program work.

    First you should replace unordered_map<id, City_details> info_map; with unordered_map<std::string, City_details> info_map;

    Second you had a missing semicolon ; for you add_data member function declaration inside the class which you might have missed while copy pasting the code here on Stackoverflow.

    Third i have avoided the use of using namespace std;.

    main.cpp

    #include <iostream>
    #include "file.h"
    int main()
    {
      
       Citydata c1;
        c1.add_data("id1","Georgia", 34);
        c1.add_data("id2", "California", 32);
        c1.add_data("id3","Texas", 23);
        //lets print out the element of info_map
        for(auto &it:c1.info_map)
            std::cout << it.first <<" "<<it.second.name<<" "<<it.second.taxrate<<std::endl;
        return 0;
    }
    

    file.h

    #pragma once
    #include <unordered_map>
    #include <string>
    struct City_details {
       std::string name;
       int taxrate;
    };
    
    class Citydata {
    public:
       bool add_data(std::string id, std::string name, int taxrate);
       //other member functions...
    //private:
       std::unordered_map<std::string, City_details> info_map;//the first tempate argument should be int and not id
    };
    

    file.cpp

    #include "file.h"
    bool Citydata::add_data(std::string id, std::string name, int taxes) {
       if ( info_map.find(id) == info_map.end()) {
           City_details dataload;
           dataload.name = name;
           dataload.taxrate = taxes;
           info_map[id] = dataload;
           return true;
       }
       else return false;
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum