Create a sequence that contains the midpoints between the values in two other sequences.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double reciprocal(double val);
template<class T> void show(const char *msg, vector<T> vect);
int main(){
int i;
vector<double> v;
for(i=1; i < 10; ++i)
v.push_back((double)i);
show("Initial contents of v:", v);
cout << endl;
vector<double> v2(10);
transform(v.begin(), v.end(), v2.begin(), reciprocal);
show("v2:", v2);
return 0;
}
template<class T> void show(const char *msg, vector<T> vect) {
cout << msg << endl;
for(unsigned i=0; i < vect.size(); ++i)
cout << vect[i] << endl;
}
// Return the reciprocal
double reciprocal(double val) {
if(val == 0.0)
return 0.0;
return 1.0 / val;
}
Related examples in the same category