1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <numeric>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;
class Cat
{
std::string d_sep;
public:
Cat(string const &sep)
:
d_sep(sep)
{}
string operator()
(string const &s1, string const &s2) const
{
return s1 + d_sep + s2;
}
};
int main()
{
size_t ia1[] = {1, 2, 3, 4, 5, 6, 7};
size_t ia2[] = {7, 6, 5, 4, 3, 2, 1};
size_t init = 0;
cout << "The sum of all squares in ";
copy(ia1, ia1 + 7, ostream_iterator<size_t>{ cout, " " });
cout << "is " <<
inner_product(ia1, ia1 + 7, ia1, init) << '\n';
cout << "The sum of all cross-products in ";
copy(ia1, ia1 + 7, ostream_iterator<size_t>{ cout, " " });
cout << "and ";
copy(ia2, ia2 + 7, ostream_iterator<size_t>{ cout, " " });
cout << "is " <<
inner_product(ia1, ia1 + 7, ia2, init) << '\n';
string names1[] = {"Frank", "Karel", "Piet"};
string names2[] = {"Brokken", "Kubat", "Plomp"};
cout << "A list of all combined names in ";
copy(names1, names1 + 3, ostream_iterator<string>{ cout, " " });
cout << "and\n";
copy(names2, names2 + 3, ostream_iterator<string>{ cout, " " });
cout << "is:" <<
inner_product(names1, names1 + 3, names2, string{ "\t" },
Cat{ "\n\t"}, Cat{ " " }) <<
'\n';
}
/*
Displays:
The sum of all squares in 1 2 3 4 5 6 7 is 140
The sum of all cross-products in 1 2 3 4 5 6 7 and 7 6 5 4 3 2 1 is 84
A list of all combined names in Frank Karel Piet and
Brokken Kubat Plomp is:
Frank Brokken
Karel Kubat
Piet Plomp
*/
|