June 2018
Intermediate to advanced
348 pages
8h 45m
English
Now that we understand the basics of Stream programming as envisaged by the Stream library, let's write a piece of code that computes the average of numbers stored in a std::vector container:
//--------------- Streams_Second.cpp
// g++ -I./Streams-master/sources Streams_Second.cpp
//
#include "Stream.h"
#include <ioStream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace Stream;
using namespace Stream::op;
int main() {
std::vector<double> a = { 10,20,30,40,50 };
//------------ Make a Stream and reduce
auto val = MakeStream::from(a) | reduce(std::plus<void>());
//------ Compute the arithematic average
cout << val/a.size() << endl;
}
The previous ...