我試圖創建一個從他們的長度排列文件中讀取的單詞列表。爲此,我試圖用自定義比較器來使用std :: set。使用自定義比較器與std ::設置
class Longer {
public:
bool operator() (const string& a, const string& b)
{ return a.size() > b.size();}
};
set<string, Longer> make_dictionary (const string& ifile){
// produces a map of words in 'ifile' sorted by their length
ifstream ifs {ifile};
if (!ifs) throw runtime_error ("couldn't open file for reading");
string word;
set<string, Longer> words;
while (ifs >> word){
strip(word);
tolower(word);
words.insert(word);
}
remove_plurals(words);
if (ifs.eof()){
return words;
}
else
throw runtime_error ("input failed");
}
從這裏,我期望一個文件中的所有單詞按其長度排列的列表。相反,我得到一個非常短名單,可以精確到一個字的輸入上出現的每一個長度:
polynomially-decidable
complexity-theoretic
linearly-decidable
lexicographically
alternating-time
finite-variable
newenvironment
documentclass
binoppenalty
investigate
usepackage
corollary
latexsym
article
remark
logic
12pt
box
on
a
怎麼在這裏上的任何想法?
請發表你的程序的只有*有關*一部分作爲[MCVE(http://stackoverflow.com/help/mcve)。 – Csq 2014-09-26 08:57:28