将字符表作为键插入到文件中的浮点数中

Inserting table of chars as key and float as a result into a map from file

提问人:Ordex_ 提问时间:6/27/2023 更新时间:6/27/2023 访问量:29

问:

我想将文本文件中的值插入到地图中,但遇到了以下错误:

错误:用作初始值设定项的数组 1817年 |: first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),

fstream DistanceFile;

map<char[2], float> odleglosci;

void loadkeysdistance()
{
    char temp[2];
    string templine = "";
    float tempfloat = 0;
    DistanceFile.open("keys.txt");
    while (!DistanceFile.eof())
    {
        getline(disctionary, templine);
        temp[0] = templine[0];
        temp[1] = templine[2];
        tempfloat = stof(templine.substr(4, templine.length() - 4));
        odleglosci[temp] = tempfloat;
    }
}

这是我正在使用的功能,文本文件“keys.txt”看起来像这样:

A A 0
A B 87.82
A C 51.29
A D 38.10
A E 38.40
A F 57.15
A G 76.20
A H 95.25

依此类推,直到到达 ZZ

C++ 字典 文件 错误处理

评论


答:

2赞 Quimby 6/27/2023 #1

std::map<char[2], float> odleglosci;这不会像您想象的那样做。

它会尝试比较指向数组的第一个元素的指针,而不是内容。

odleglosci[temp] = tempfloat;由于上述原因没有意义,并且由于某种不相关的原因在 clang 上触发编译错误,因为您没有发布最小的可重复示例,也没有发布完整的错误,我不确定究竟是什么原因导致了您的错误。

无论如何,请改用。std::pair<char,char>