繁体   English   中英

获取ifstream将文件中的整数读取到映射中C ++

[英]Getting ifstream to read ints from a file into a map C++

我似乎无法让我的文件“ paths.txt”读入我的地图文件。 我无法弄清楚我在做什么错。 我将不胜感激任何指针。 我想要的最终结果是将每个键值对带入映射。

我文件的格式是

192,16 120,134 256,87 122,167 142,97 157,130 245,232 223,63 107,217 36,63 206,179 246,8 91,178

我的代码是

ifstream myfile("paths.txt"); 
    std::map<int, int> mymap;
    int a, b;
    while (myfile.good())
    {
    myfile >> a >> b;
    mymap[a] = b;
    }
    mymap;
  1. 您没有任何内容可读取文本文件中的逗号。
  2. 另外,使用while ( myfile >> ...)代替while (myfile.good()) while ( myfile >> ...)
std::map<int, int> mymap;

char dummy;
int a, b;
while (myfile >> a >> dummy >> b)
{
   mymap[a] = b;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM