代碼從C++引擎(3第三個)。 錯誤是:在Ubuntu Linux上使用g ++編譯引擎示例代碼時出錯
* filterString.cpp:在函數 '詮釋主()': filterString.cpp:32:68:錯誤:不能轉換 '__gnu_cxx :: __ normal_iterator *,性病::矢量>>'到'std :: string * {aka std :: basic_string }'在初始化中
請幫我分析一下錯誤, 謝謝。
代碼:
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
template <class InputIterator>
void filter_string(InputIterator first, InputIterator last, string filt_elems = string("\",?.")) {
for (; first != last; first++){
string:: size_type pos = 0;
while ((pos = (*first).find_first_of(filt_elems, pos)) != string::npos)
(*first).erase(pos, 1);
}
}
bool length_less (string s1, string s2) {
return s1.size() < s2.size();
}
int main() {
istream_iterator<string> input(cin), eos;
vector<string> text;
copy(input, eos, back_inserter(text));
string filt_elems("\",.?;:");
filter_string(text.begin(), text.end(), filt_elems);
int cnt = text.size();
string *max = max_element(text.begin(), text.end(), length_less);
int len = max->size();
cout << "The number of words read is " << cnt << endl;
cout << "The longest word has a length of " << len << endl;
cout << "The longest word is " << *max << endl;
return 0;
}
如果您正在從一本書或其他舊的(互聯網時間)來源開始工作,您可能正在處理對事物如何運作的理解的變化,因爲語言已經得到了更好的指定。設置一個'-std ='可以讓你在編譯器中使用一個更早的理解。 – dmckee
對不起,我完全無法理解。尤其是這樣的話:「設置-std =可能讓你在編譯器中使用更早的理解」。你能否從一點到另一點解釋它!非常感謝! –
G ++採用'std = C++ 98'或'std = C++ = 0x'形式的參數。選擇正確的*可能會得到g ++來處理代碼。 – dmckee