2014-01-21 38 views
0

我有C++代碼。它讀取文件並在此文件中查找字符(「diction」)。在文件中排序C++

while (1) 
{ 
    infile.getline(buff, sizeof(buff)); 
    if(infile.eof()) break; 
    sbuff = buff; 
    sort(sbuff.begin(), sbuff.end()); 
    an.insert(pair<string, string>(sbuff, buff)); 
} 

im = an.begin(); 
ane = an.end(); 

vector<multimap<string, string>::iterator> chg; 

chg.push_back(im); 

while (++im != ane) 
{ 
    chg.push_back(im); 
    if((*im).first != (*chg[0]).first) 
    { 
     if(chg.size() > 2) 
     { 
      for(unsigned int i=0; i < chg.size() - 1; i++); 
     cout << endl; 
     } 

     chg.clear(); 
     chg.push_back(im); 
    } 

} 

if(chg.size() > 1) 
    for(unsigned int i=0; i < chg.size(); i++) 
     cout << (*chg[i]).second << endl; 
    infile.close(); 
} 

編譯後顯示空行和所有行的早午餐。 在Fedora上的MacOS上,結果相同

什麼是錯誤?

+0

代碼註釋是 「不對勁」 – noelicus

回答

2

for循環在你的代碼沒有做任何事情:

{ 
    for(unsigned int i=0; i < chg.size() - 1; i++); 
    cout << endl; 
} 

你不想;在它的結束,大概。如果你把它拿走,它會給你很多空行,儘管你不會在屏幕上顯示任何文字。

+0

非常感謝你! 非常感謝!我刪除;最後。 並在此後添加此行: 'cout <<(* chg [i])。second << endl;' 它工作正常! – ReDSerpenT

1

我的事情你錯過了{

試試這個

if(chg.size() > 1) 
{ 
    for(unsigned int i=0; i < chg.size(); i++) 
    { 
     cout << (*chg[i]).second << endl; 
    } 
    infile.close(); 
} 

代替

if(chg.size() > 1) 
    for(unsigned int i=0; i < chg.size(); i++) 
     cout << (*chg[i]).second << endl; 
    infile.close(); 
} 
+0

謝謝!在這種情況下,大括號沒有必要。但它正在工作,因爲我不''。雖然他們提高了代碼的可讀性。 – ReDSerpenT