将字符表作为键和浮点数作为结果插入到地图中,从文件中。

huangapple go评论59阅读模式
英文:

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

问题

我想将文本文件中的值插入到一个映射中,遇到了这个错误:

> error: array used as initializer
> 1817 | : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),

这是我正在使用的函数,文本文件"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。

英文:

I want to insert values from text file into a map and encountered this error:
> error: array used as initializer
> 1817 | : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),

fstream DistanceFile;

map&lt;char[2], float&gt; odleglosci;

void loadkeysdistance()
{
    char temp[2];
    string templine = &quot;&quot;;
    float tempfloat = 0;
    DistanceFile.open(&quot;keys.txt&quot;);
    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;
    }
}

this is the function i am using and text file "keys.txt" looks like that:

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

and so on till it gets to ZZ

答案1

得分: 2

std::map&lt;char[2], float&gt; odleglosci; 这不是你想要的效果。

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

odleglosci[temp] = tempfloat; 由于上述原因没有意义,并且会因为某种不相关的原因在clang上触发编译错误。因为你没有发布一个最小可重现的示例或完整的错误信息,所以我不确定你的问题具体是什么。

无论如何,应该使用 std::pair&lt;char,char&gt; 代替。

英文:

std::map&lt;char[2], float&gt; odleglosci; this does not do what you think it does.

It would try to compare the pointers to the first elements of the arrays, not the content.

odleglosci[temp] = tempfloat; does not make sense due to above and triggers a compilation error on clang for somewhat unrelated reason, since you did not post a minimal reproducible example nor the full error, I am not sure what is causing yours exactly.

Anyway, use std::pair&lt;char,char&gt; instead.

huangapple
  • 本文由 发表于 2023年6月27日 21:47:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定