unordered_map不正确使用或错误

unordered_map used incorrectly or a bug?

本文关键字:错误 不正确 map unordered      更新时间:2023-10-16

嘿,我需要立即帮助...我通常使用C#,但必须在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>,但是我会遇到一系列错误,我真的没有得到:

错误1错误c2039:'iterator_category':不是'std :: basic_string&lt; _ELEM,_traits,_ax>'c: program files(x86) microsoft Visualio 10.0 10.0 vc incless xutility 373 1 cpptp

和其他5个...正如我一个小时前只知道这些功能的那样,我假定它的错误用法或通过参考的呼叫不是仪式...因此

使用:

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

或:

hashtable.emplace(name, customerPurchaseArray);

或:

hashtable[name] = customerPurchaseArray;

请注意,有一个区别:前两个将不更改任何现有元素,而最后一个元素总是无条件地覆盖现有元素。