2015-10-14 85 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);