在c++中将csv读入unordered_map
Reading a csv into a unordered_map in C++
我试图读取csv文件并将其存储在c++中的哈希映射中。这是我的代码。
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);
std::getline
:
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
std::basic_string<CharT,Traits,Allocator>& str,
CharT delim );
std::getline
的第二个参数必须是std::string
。
因此,将value
读取为std::string
并将其转换为int
。
std::string _value;
int value;
std::getline(openFile,_value,'n');
value = std::stoi(_value);
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- unordered map -在c++ std::unordered_map中预分配桶