英文:
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<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;
}
}
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<char[2], float> odleglosci;
这不是你想要的效果。
它会尝试比较数组的第一个元素的指针,而不是内容。
odleglosci[temp] = tempfloat;
由于上述原因没有意义,并且会因为某种不相关的原因在clang上触发编译错误。因为你没有发布一个最小可重现的示例或完整的错误信息,所以我不确定你的问题具体是什么。
无论如何,应该使用 std::pair<char,char>
代替。
英文:
std::map<char[2], float> 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<char,char>
instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论