2015-05-08 20 views
2

以下代碼來自本書:C++標準庫第二版。在控制檯中鍵入什麼以觸發流結束?

如何在Mac Yosemite上運行它?

在控制檯中,我輸入a,輸入b,輸入...沒有結束....如何輸入流結束?

/* The following code example is taken from the book 
* "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 
* by Nicolai M. Josuttis, Addison-Wesley, 2012 
* 
* (C) Copyright Nicolai M. Josuttis 2012. 
* Permission to copy, use, modify, sell and distribute this software 
* is granted provided this copyright notice appears in all copies. 
* This software is provided "as is" without express or implied 
* warranty, and with no claim as to its suitability for any purpose. 
*/ 
#include <iterator> 
#include <algorithm> 
#include <vector> 
#include <string> 
#include <iostream> 
using namespace std; 

int main() 
{ 
    vector<string> coll; 

    // read all words from the standard input 
    // - source: all strings until end-of-file (or error) 
    // - destination: coll (inserting) 
    copy (istream_iterator<string>(cin), // start of source 
      istream_iterator<string>(),  // end of source 
      back_inserter(coll));    // destination 

    // sort elements 
    sort (coll.begin(), coll.end()); 

    // print all elements without duplicates 
    // - source: coll 
    // - destination: standard output (with newline between elements) 
    unique_copy (coll.cbegin(), coll.cend(),   // source 
       ostream_iterator<string>(cout,"\n")); // destination 
} 
+1

「文件終端」應該是網絡搜索結果。 – chris

+0

這取決於你正在運行的操作系統。 –

回答

0

Ctrl-D在Mac OS和其他UNIX衍生產品上。

+0

它在XCode中工作,但不能與「cpp.sh」聯機工作。 – milesma

+0

不知道如何在cpp.sh Web界面上做到這一點。如果你不行,不會感到驚訝。 – sfjac

相關問題