C++ STL : An example of Transform Algorithm
#include <iostream>
#include <algorithm>
#include <list>
double reciprocal(double i){
return 1.0/i;
}
int main()
{
std::list<double> vals;
int i;
for(i=1; i<10; i++) vals.push_back((double)i);
std::cout << "Original contents of vals:\n";
std::list<double>::iterator p = vals.begin();
while(p != vals.end()) {
std::cout << *p << " ";
p++;
}
std::cout << std::endl;
p = transform(vals.begin(), vals.end(),
vals.begin(), reciprocal);
std::cout << "Transformed contents of vals:\n";
p = vals.begin();
while(p != vals.end()) {
std::cout << *p << " ";
p++;
}
return 0;
}
#include <algorithm>
#include <list>
double reciprocal(double i){
return 1.0/i;
}
int main()
{
std::list<double> vals;
int i;
for(i=1; i<10; i++) vals.push_back((double)i);
std::cout << "Original contents of vals:\n";
std::list<double>::iterator p = vals.begin();
while(p != vals.end()) {
std::cout << *p << " ";
p++;
}
std::cout << std::endl;
p = transform(vals.begin(), vals.end(),
vals.begin(), reciprocal);
std::cout << "Transformed contents of vals:\n";
p = vals.begin();
while(p != vals.end()) {
std::cout << *p << " ";
p++;
}
return 0;
}
Reference C++: The Complete Reference, 4th Edition
You may also like:
How do I learn competitive programming as a beginner?
What to use endl or "\n"
C++: Inheritance
C++: Program to find prime no.
C++: Implementation of Heapsort