Advance istream_iterator
/* 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.
*/
#include <iostream>
#include <string>
#include <algorithm>
#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
istream_iterator reads and displays characters from cin until a period is received.
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<char> in_it(cin);
do {
cout << *in_it++;
} while (*in_it != ".");
return 0;
}
Read string from keyboard and save to vector directly
#include <iostream>
#include <istream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#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
Read words from standard input, sort and print out without duplicates
/* 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.
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#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
Taking the sum of values from a stream
#include <iostream>
#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
Use istream_iterator to loop through a string
#include <iostream>
#include <iterator>
#include <string>
#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
Use istream_iterator to loop through a string defined by char pointer
#include <iostream>
#include <iterator>
#include <string>
#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