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 << " ";
   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 << " ";
    return 0;

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

Popular Posts

C++: Selection sort using STL

Top 10 Web Sites Every Programmer Should Visit Daily