C++/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;

} /* Terminate batch job (Y/N)? n

*/        
 </source>


Read string from keyboard and save to vector directly

<source lang="cpp">

  1. include <iostream>
  2. include <istream>
  3. include <string>
  4. include <vector>
  5. include <algorithm>
  6. include <functional>
  7. 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;

} /* Enter a series of strings: a b c a s c de foo

  • p = s

Terminate batch job (Y/N)? n

*/        
 </source>


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

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

*/        
 </source>


Taking the sum of values from a stream

<source lang="cpp">

  1. include <iostream>
  2. 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; } /* Ctrl-Z to stop ^Z The sum value is 0

*/        
 </source>


Use istream_iterator to loop through a string

<source lang="cpp">

  1. include <iostream>
  2. include <iterator>
  3. include <string>
  4. 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;

} /* 4.5 6.75 8.25 7.5 5.75 32.75

*/        
 </source>


Use istream_iterator to loop through a string defined by char pointer

<source lang="cpp">

  1. include <iostream>
  2. include <iterator>
  3. include <string>
  4. 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;

} /* 4.5 6.75 8.25 7.5 5.75 32.75

*/        
 </source>