2015-10-14 24 views
2

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

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'); 

我在做什么错。

回答

0

std::getline预计std::string作为其第二个参数。您应该将std::string对象传递给getline,然后使用std::stoi将该字符串转换为int

像这样:

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

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第二ARG必须是std::string
因此,请将value设为std::string并将其转换为int

std::string _value; 
int value; 
std::getline(openFile,_value,'\n'); 
value = std::stoi(_value);