gpt4 book ai didi

c++ - 在 C++ 中将 csv 读入 unordered_map

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:48:49 24 4
gpt4 key购买 nike

我正在尝试读取一个 csv 文件并将其存储在 C++ 中的 HashMap 中。这是我的代码。

 void processList(std::string path, std::unordered_map<std::string, int> rooms){

std::ifstream openFile(path);
std::string key;
int value;
std::getline(openFile, key, ','); //to overwrite the value of the first line
while(true)
{
if (!std::getline(openFile, key, ',')) break;

std::getline(openFile, value, '\n');
rooms[key] = value;
std::cout << key << ":" << value << std::endl;
}
}

我不断收到以下错误

error: no matching function for call to 'getline'
std::getline(openFile, value, '\n');

我做错了什么。

最佳答案

std::getline 需要一个 std::string 作为它的第二个参数。您应该将 std::string 对象传递给 getline,然后使用 std::stoi 将该字符串转换为 int.

像这样:

std::string valueString;
std::getline(openFile, valueString, '\n');
auto value = std::stoi(valueString);

关于c++ - 在 C++ 中将 csv 读入 unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com