我想使用流迭代器從控制檯讀取和輸出單詞。這裏是我的嘗試:C++流迭代器
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
vector<string> stringVec;
copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(stringVec));
sort(stringVec.begin(), stringVec.end());
unique_copy(stringVec.cbegin(), stringVec.cend(), ostream_iterator<string> (cout, "\n"));
return 0;
}
當我輸入「這是它」並按回車鍵在控制檯,那裏閃爍保持光標(表明它在等待輸入)。
任何人都可以提供一些見解我的方法嗎?
在此先感謝。
我需要使用getline作爲解決方法嗎? – Roy
我不能說你要使用它,但它是一種簡單而標準的方式。你也可以用任何自定義流來替換'cin'。 – deepmax
謝謝。上面的代碼不會在我身上編譯。 VS表示以下錯誤: IntelliSense:沒有構造函數的實例「std :: istream_iterator <_Ty,_Elem,_Traits,_Diff> :: istream_iterator [使用_Ty = std :: string,_Elem = char,_Traits = std :: char_traits,_Diff = ptrdiff_t]「匹配參數列表 參數類型爲:(std :: istringstream)」 –
Roy