C++ Tutorial/STL Algorithms Iterator/istream iterator

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Advance istream_iterator

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* 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.
*/
  1. include <iostream>
  2. include <string>
  3. include <algorithm>
  4. include <iterator>

using namespace std; int main() {

   istream_iterator<string> cinPos(cin);
   ostream_iterator<string> coutPos(cout," ");
   /* while input is not at the end of the file
    * - write every third string
    */
   while (cinPos != istream_iterator<string>()) {
       // ignore the following two strings
       advance (cinPos, 2);
       // read and write the third string
       if (cinPos != istream_iterator<string>()) {
           *coutPos++ = *cinPos++;
       }
   }
   cout << endl;

}</source>

Terminate batch job (Y/N)? n

istream_iterator reads and displays characters from cin until a period is received.

<source lang="cpp">#include <iostream>

  1. include <iterator>

using namespace std;

int main() {

 istream_iterator<char> in_it(cin);
  
 do {
   cout << *in_it++;
 } while (*in_it != ".");
  
 return 0;

}</source>

Read string from keyboard and save to vector directly

<source lang="cpp">#include <iostream>

  1. include <istream>
  2. include <string>
  3. include <vector>
  4. include <algorithm>
  5. include <functional>
  6. include <iterator>

using namespace std; int main( ) {

  cout << "Enter a series of strings: ";
  istream_iterator<string> start(cin);
  istream_iterator<string> end;
  vector<string> v(start, end);
  vector<string>::iterator p = partition(v.begin( ), v.end( ),bind2nd(less<string>( ), "foo"));
  cout << "*p = " << *p << endl;

}</source>

Enter a series of strings: a b c
a s c
de
foo
*p = s
Terminate batch job (Y/N)? n

Read words from standard input, sort and print out without duplicates

<source lang="cpp">/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* 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.
*/
  1. include <iostream>
  2. include <vector>
  3. include <string>
  4. include <algorithm>
  5. include <iterator>

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.begin(), coll.end(),             // source
                ostream_iterator<string>(cout,"\n")); // destination

}</source>

a b c
de
d
s
s
e
w
d
a
b
c^CTerminate batch job (Y/N)? n

Taking the sum of values from a stream

<source lang="cpp">#include <iostream>

  1. include <iterator>

using std::cout; using std::endl; using std::cin; using std::istream_iterator; template <typename Iter> double mySum (Iter begin, Iter end) {

 double sum = 0.0;
 
 for( ; begin != end ;)
   sum += *begin++;
 return sum;

} int main() {

 cout << "Ctrl-Z to stop" << endl;
 double av = mySum(istream_iterator<double>(cin), istream_iterator<double>());
 cout << "The sum value is " << av << endl;

return 0; }</source>

Ctrl-Z to stop
^Z
The sum value is 0

Use istream_iterator to loop through a string

<source lang="cpp">#include <iostream>

  1. include <iterator>
  2. include <string>
  3. include <sstream>

using std::cout; using std::endl; using std::istream_iterator; using std::istringstream; using std::string; template <typename Iter> double mySum(Iter begin, Iter end) {

 double sum = 0.0;
 
 for( ; begin != end ;)
   sum += *begin++;
 return sum;

} int main() {

 std::string stock_ticker = "4.5 6.75 8.25 7.5 5.75";
 istringstream ticker(stock_ticker);
 istream_iterator<double> begin(ticker);
 istream_iterator<double> end;
 cout << stock_ticker << endl ;
 
 cout << mySum (begin, end) << endl; 
 return 0;

}</source>

4.5 6.75 8.25 7.5 5.75
32.75

Use istream_iterator to loop through a string defined by char pointer

<source lang="cpp">#include <iostream>

  1. include <iterator>
  2. include <string>
  3. include <sstream>

using std::cout; using std::endl; using std::istream_iterator; using std::istringstream; using std::string; template <typename Iter> double mySum(Iter begin, Iter end) {

 double sum = 0.0;
 
 for( ; begin != end ;)
   sum += *begin++;
 return sum;

} int main() {

 char* stock_ticker = "4.5 6.75 8.25 7.5 5.75";
 
 istringstream ticker(stock_ticker);
 istream_iterator<double> begin(ticker);
 istream_iterator<double> end;
 cout << stock_ticker << endl ;
 
 cout << mySum (begin, end) << endl; 
 return 0;

}</source>

4.5 6.75 8.25 7.5 5.75
32.75