获取 ifstream 以将文件中的整数读入映射 C++
Getting ifstream to read ints from a file into a map C++
我似乎无法让我的文件 "paths.txt" 读入我的地图文件。我不知道我做错了什么。我会很感激任何指示。我想要的最终结果是把每一个键值对都放到map中。
我的文件格式是
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;
- 您没有任何内容可以读取文本文件中的逗号。
- 此外,使用
while ( myfile >> ...)
. 而不是 while (myfile.good())
std::map<int, int> mymap;
char dummy;
int a, b;
while (myfile >> a >> dummy >> b)
{
mymap[a] = b;
}
我似乎无法让我的文件 "paths.txt" 读入我的地图文件。我不知道我做错了什么。我会很感激任何指示。我想要的最终结果是把每一个键值对都放到map中。
我的文件格式是
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;
- 您没有任何内容可以读取文本文件中的逗号。
- 此外,使用
while ( myfile >> ...)
. 而不是
while (myfile.good())
std::map<int, int> mymap;
char dummy;
int a, b;
while (myfile >> a >> dummy >> b)
{
mymap[a] = b;
}