简体   繁体   English

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

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

I cannot seem to get my file "paths.txt" to read into my map file. 我似乎无法让我的文件“ paths.txt”读入我的地图文件。 I cannot figure out what I am doing wrong. 我无法弄清楚我在做什么错。 I'd appreciate any pointers. 我将不胜感激任何指针。 The end result I want is to take each of the key value pairs into the map. 我想要的最终结果是将每个键值对带入映射。

The format of my file is 我文件的格式是

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 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

And my code is 我的代码是

ifstream myfile("paths.txt"); 
    std::map<int, int> mymap;
    int a, b;
    while (myfile.good())
    {
    myfile >> a >> b;
    mymap[a] = b;
    }
    mymap;
  1. You don't have anything to read the commas in your text file. 您没有任何内容可读取文本文件中的逗号。
  2. Also , instead of while (myfile.good()) , use while ( myfile >> ...) . 另外,使用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