// Code using the input and output STL iterators.
#include <iostream.h>
#include <iterator>
int main() {
int number1, number2;
cout << "Enter two integers: ";
istream_iterator<int> inputInt(cin);
number1 = *inputInt; // read first int from standard in
++inputInt; //move iterator to the next input value
number2 = *inputInt; // get next
ostream_iterator<int> outputInt(cout);
cout << "The sum is: ";
*outputInt = number1+number2; // output result to standard out
cout << endl;
return 0;
}
Enter two integers: 12 25
The sum is: 37
//Code using deque from the STL
#include <iostream.h>
#include <deque>
#include <algorithm>
int main() {
deque<double> values;
ostream_iterator<double> output(cout," ");
values.push_front(2.2);
values.push_front(3.5);
values.push_back(1.1);
cout << "values contains: ";
for ( int i=0; i < values.size(); i++)
cout << values[i] << ' ';
values.pop_front();
cout << endl << "After pop_front values contains: ";
copy(values.begin(), values.end(), output);
values[1] = 5.4;
cout << endl << "After values[1] = 5.4 values contains: ";
copy (values.begin(), values.end(), output);
cout << endl;
return 0;
}
values contains: 3.5 2.2 1.1
After popping the front values contains: 2.2 1.1
After values[1] = 5.4 values contains: 2.2 5.4
// Code using Vector, accumulate, and some algorithms
#include <iostream.h>
#include <algorithm>
#include <numeric>
#include <vector>
bool greater9(int value) {return value<9; }
void outputSquare(int value) { cout << value*value << ' '; }
int calculateCube(int value) { return value*value*value; }
int main() {
const int SIZE = 10;
int a1[] = {100, 2, 8, 1, 50, 3, 8, 8, 9, 10};
vector<int> v(a1,a1+SIZE);
ostream_iterator<int> output(cout, " ");
cout << "Vector v: ";
copy(v.begin(),v.end(),output);
sort(v.begin(),v.end());
cout << endl << "Vector v after sort: ";
copy(v.begin(),v.end(),output);
random_shuffle(v.begin(),v.end());
cout << endl << "Vector v after random_shuffle: ";
copy(v.begin(),v.end(),output);
int result = 0;
count(v.begin(), v.end(), 8, result);
cout << endl << "Number of elements matching 8: " << result;
count_if(v.begin(), v.end(), greater9, result);
cout << endl << "Number of elements greater than 9: " << result;
cout << endl << "Minimum element in Vector v is: "
<< *(min_element(v.begin(), v.end()));
cout << endl << "Maximum element in Vector v is: "
<< *(max_element(v.begin(), v.end()));
cout << endl << "The total of the elements in Vector v is: "
<< accumulate(v.begin(), v.end(), 0);
cout << endl << "The square of every integer in Vector v is: " << endl;
for_each(v.begin(), v.end(), outputSquare);
vector<int> cubes(SIZE);
transform(v.begin(), v.end(), cubes.begin(), calculateCube);
cout << endl << "The cube of every integer in Vector v is: " << endl;
copy(cubes.begin(), cubes.end(), output);
cout << endl;
return 0;
}
Vector v: 100 2 8 1 50 3 8 8 9 10
Vector v after sort: 1 2 3 8 8 8 9 10 50 100
Vector v after random_shuffle: 9 50 100 3 2 8 8 10 1 8
Number of elements matching 8: 3
Number of elements greater than 9: 3
Minimum element in Vector v is: 1
Maximum element in Vector v is: 100
The total of the elements in Vector v is: 199
The square of every integer in Vector v is:
81 2500 10000 9 4 64 64 100 1 64
The cube of every integer in Vector v is:
729 125000 1000000 27 8 512 512 1000 1 512