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
| //std
#include <vector>
#include <iostream>
#include <iterator>
using namespace std;
template <class VectorType, typename Function>
VectorType eval(const VectorType& v1, const VectorType& v2, Function func)
{
VectorType v;
auto it1 = v1.begin(), it2 = v2.begin();
auto end1 = v1.end(), end2 = v2.end();
for (;it1!=end1 && it2!=end2; ++it1, ++it2)
{
v.push_back(func(*it1,*it2));
}
return v;
}
template <typename Iterable>
void display(const Iterable& v)
{
ostream_iterator<typename Iterable::value_type> out_it (cout," ");
copy( v.begin(), v.end(), out_it );
cout << endl;
}
int main(int argc, char* argv[])
{
vector<int> a = { 1, 2, 3, 4, 5, 6 };
vector<int> b = { 1, 0, 1, 0, 1, 0 };
vector<int> c;
c = eval( a, b, [] (int a, int b) { return a+b; } );
display(c);
c = eval( a, b, [] (int a, int b) { return a*b; } );
display(c);
return 0;
} |
Partager