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

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

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

问题

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

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

这是我正在使用的函数,文本文件"keys.txt"如下所示:

  1. A A 0
  2. A B 87.82
  3. A C 51.29
  4. A D 38.10
  5. A E 38.40
  6. A F 57.15
  7. A G 76.20
  8. 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))...),

  1. fstream DistanceFile;
  2. map&lt;char[2], float&gt; odleglosci;
  3. void loadkeysdistance()
  4. {
  5. char temp[2];
  6. string templine = &quot;&quot;;
  7. float tempfloat = 0;
  8. DistanceFile.open(&quot;keys.txt&quot;);
  9. while (!DistanceFile.eof())
  10. {
  11. getline(disctionary, templine);
  12. temp[0] = templine[0];
  13. temp[1] = templine[2];
  14. tempfloat = stof(templine.substr(4, templine.length() - 4));
  15. odleglosci[temp] = tempfloat;
  16. }
  17. }

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

  1. A A 0
  2. A B 87.82
  3. A C 51.29
  4. A D 38.10
  5. A E 38.40
  6. A F 57.15
  7. A G 76.20
  8. 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:

确定