2009-06-18 69 views
1

我正在使用Stroustrup的天鵝書。我遇到了從 載體獲得輸出的問題。我跟着第二個文本例子。 4.6.3在第121頁。我 設法讓源編譯並能夠執行它。在 之後鍵入空白分隔的單詞列表,程序掛起並且 不會按照它應該列出矢量的元素。我意識到不是 如果重複,每個元素都將被輸出,但是我根本沒有收到 輸出。我編譯並運行了這個程序,它使用Linux上的g ++ 4.3.2 編譯器,以及使用Windows的 窗口上的Visual C++ express 2008編譯器。兩者產生相同的結果。感謝您抽出時間來 閱讀此。這裏是我的源代碼:Stroustrup天鵝書矢量問題

#include "Supporting_files/std_lib_facilities.h" 

    int main() 
      { 
     vector<string> words; 
     string temp; 
     cout << "Enter a list of words: "; 
     while(cin>>temp) 
      words.push_back(temp); 
      cout << "Number of words: " << words.size() << endl; 
      sort(words.begin(),words.end()); 
      for(int i=0;i<words.size();++i) 
       if(i==0||words[i-1]!=words[i]) 
        cout << words[i] << "\n"; 
      } 
+0

我遇到了Stroustrup ... mpen 2009-06-18 21:29:33

回答

5

while(cin >> temp)只在文件結束時結束。使用control-D將文件的末尾發送到終端。