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 0 -1 -1 -1 1 5 0 -2 -2 
V 0 -1 -3 -2 -1 -3 -3 3 -2 1 1 -3 -2 -2 -3 -2 0 4 -3 -1 
W -3 -2 -4 -3 1 -2 -2 -3 -3 -2 -1 -4 -4 -2 -3 -3 -2 -3 11 2 
Y -2 -2 -3 -2 3 -3 2 -1 -2 -1 -1 -2 -3 -1 -2 -2 -2 -1 2 7 

그러나지도

static void gen_matrix(map<string, int>& mat) 
{ 
    const string myfile = "myfile.dat"; 
    vector<string> myChars = { "A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "Y" }; 
    ifstream f(myfile); 
    string temporal; 
    getline(f, temporal); // Ignore first string 
    while (getline(f, temporal)) 
    { 
    auto buff(split_string(temporal)); 
    auto prefix = buff[0]; 
    size_t i = 1; 
    //try 1 
    // for(auto it = myChars.begin(), end = myChars.end(); it != end; ++it){ 
    //  mat[prefix + it] = stoi(buff[i++]); 
    // } 
    //try 2  
    //for(auto& var = 0; myChars){ 
    //  mat[prefix + var] = stoi(buff[i++]); 
    //} 
    } 
} 
을 채울 때 내가 루프에 문제가 있어요 내가 달성하고자하는 것을

각 기능

for each (const auto& var in myChars) 
    { 
     mat[prefix + var] = stoi(buff[i++]); 
    } 

그러나 더 succes에, 어떻게 루프 변경하지했다입니다?

std::vector<std::string> split(const std::string &s) { 
    std::stringstream ss(s); 
    std::string item; 
    std::vector<std::string> elems; 
    while (ss >> item) { 
     elems.push_back(item); 
    } 
    return elems; 
} 


    std::string temporal; 
    std::getline(fin, temporal,'\r'); // You may need this carriage return as delim 
    std::map<std::string, int> mat; 
    std::vector<std::string> buff; 

    while(std::getline(f, temporal,'\r')) 
    { 
    buff = split(temporal) ; 
    for(std::size_t x=1; x < buff.size() ; ++x) 
     mat[buff[0]+myChars[x-1]] = atoi(buff[x].c_str()); 
    } 

답변

1

:

ifstream f(myfile); 
f.ignore(INT_MAX, '\n'); 
while (f.good()){ 
    string prefix; 
    f >> prefix; 
    for (const string var : myChars){ 
     int val; 
     f >> val; 
     mat[prefix + var] = val; 
    } 
    f >> ws; 
} 
1

내가 오히려이처럼 IO 연산자를 사용,의 getline을 사용하여 라인을 읽을 수 없습니다 제안 : 당신이 당신의 split_string를 표시하지 않은, 내가 제대로 이해한다면, 당신은 다음과 같은 시도 할 수 있습니다으로

관련 문제