эй, ребята, мне нужна помощь сразу... я обычно использую С#, но должен сделать код в c++, поэтому быстро проходил через полезные типы данных и процедуры. Вот код:
#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;
}
когда я застрял в этой проблеме.. здесь я пытаюсь использовать unordered_map<string, string>
но im получает серию ошибок, которые я действительно не получаю:
Ошибка 1 ошибка C2039: 'iterator_category': не является членом 'std :: basic_string <_Elem, _Traits, _Ax>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility 373 1 CPPTP
и еще 5... поскольку я только узнал об этих функциях час назад, полагая, что его просто неправильное использование или вызов по ссылке - это не обряд... так кто-нибудь даже не знает, в чем проблема и как ее решить... любые советы будут оценены...
Используйте либо:
hashtable.insert(make_pair(name, customerPurchaseArray));
Или:
hashtable.emplace(name, customerPurchaseArray);
Или:
hashtable[name] = customerPurchaseArray;
Обратите внимание, что есть разница: первые два не изменят никаких существующих элементов, тогда как последний всегда, безоговорочно перезаписывает существующие элементы.