Question

hey guys i need some help immediately... i am usually using c# but have to make a code in c++ so was quickly going through the useful datatypes and procedures Here's the code:

#include<iostream>
#include  <unordered_map>
#include <vector>
#include <string>
using namespace std;

void insertInHashtable(string customerString,unordered_map<string, string> &hashtable )
{
    string customerPurchaseArray, name;
    int i= 0, firstCommaPosition = 0;
    int length = customerString.length();
    while (i<length)
        if (customerString[i] == ',')
        {
            firstCommaPosition = i;
            break;
        }
        else
            i++;
    customerPurchaseArray.assign(customerString, firstCommaPosition + 1, string::npos);
    name.assign(customerString, 0, firstCommaPosition - 1);
    hashtable.insert(name, customerPurchaseArray);
}

int main (int args[])
{
    string value = " error...!!!";
    unordered_map<string, string> hashtable;
    string customerString = "Ayush,p1234,p345,p34,p43,p444";
    insertInHashtable(customerString, hashtable);
    unordered_map<string, string>::iterator got = hashtable.find("Ayush");
    if (got != hashtable.end())
    value = got->second;
    std::cout<<value;
    char ch;
    std::cin>>ch;
}

when i got stuck at this issue.. here im trying to use unordered_map<string, string> but im getting a series of errors which i dont really get like :

Error 1 error C2039: 'iterator_category' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 373 1 CPPTP

and 5 others... as i only got to know about these functions an hour ago im presuming that its just wrong usage or call by reference is not rite... so does anyone have any idea what the problem is and how to solve it... any advice will be appreciated...

Was it helpful?

Solution

Use either:

hashtable.insert(make_pair(name, customerPurchaseArray));

Or:

hashtable.emplace(name, customerPurchaseArray);

Or:

hashtable[name] = customerPurchaseArray;

Note that there's a difference: The first two will not change any existing elements, whereas the last one always, unconditionally overwrites existing elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top