我一直在解決加速C++練習8-5,我不想錯過本書中的單個練習。加速C++練習8-5解決方案不清晰
加速C++練習8-5如下:
從第7章重新實現
gen_sentence
和xref
功能使用 輸出迭代而不是將它們的整個輸出在一個數據 結構。通過編寫將 輸出迭代器直接附加到標準輸出的程序並將 結果分別存儲在list <string>
和map<string, vector<int> >
中來測試這些新版本。
爲了解這個問題的範圍和本書的這一部分的當前知識 - 本練習是關於泛型函數模板和模板中迭代器使用的章節的一部分。上一頁演習旨在落實<algorithm>
庫函數的簡單版本,如equal, find, copy, remove_copy_if
等
如果我理解正確的話,我需要修改xref
功能,因此它:
- 使用輸出迭代
- 商店的結果
map<string, vector<int> >
我試圖通過地圖迭代器爲back_inserter()
,.begin()
,.end()
到THI s函數,但無法編譯它。回答here解釋了原因。
外部參照的功能,如第7章:
// find all the lines that refer to each word in the input
map<string, vector<int> >
xref(istream& in,
vector<string> find_words(const string&) = split)
{
string line;
int line_number = 0;
map<string, vector<int> > ret;
// read the next line
while (getline(in, line)) {
++line_number;
// break the input line into words
vector<string> words = find_words(line);
// remember that each word occurs on the current line
for (vector<string>::const_iterator it = words.begin();
it != words.end(); ++it)
ret[*it].push_back(line_number);
}
return ret;
}
拆分實施:
vector<string> split(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters `['original value of `i', `i)'
while (i != s.size()) {
// ignore leading blanks
// invariant: characters in range `['original `i', current `i)' are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range `['original `j', current `j)' is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j) {
// copy from `s' starting at `i' and taking `j' `\-' `i' chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
請幫助理解我失去了什麼。
謝謝您的回答。很抱歉沒有發佈'split'實現。我會將其添加到我的問題。情況是我不熟悉'make_pair'函數。有一點小小的搜索發現'make_pair'只是在本書最後的庫彙總中提到的,所以對於我來說,作者試圖通過這個練習來技術化還不是很清楚。 – user1104456
make_pair是一個模板方法。如果你有這樣的類型:'std :: pair'它更容易使用:'make_pair(「hello」,5);'而不是:'std :: pair (「hello」,int)'這種類型推斷你想要做什麼樣的配對。 –
matiu
還有兩個注意事項。 1)xref函數最初返回'map>'包含int的字符串和向量,每行都出現在其上。 (答案中的附加代碼只打印出第一個行字)2)我應該可以將輸出迭代器直接附加到標準輸出。要麼我不知道該怎麼做,或者解決方案應該不同地實施? –
user1104456