Solution to exercise 4.3.1:
#define __MINMAX_DEFINED        // use STL's generic min and max templates

#include "vector.h"             // include STL vector implementation
#include "algo.h"               // include STL algorithm implementations

#include <iostream.h>

void main (void)
{
  vector<int> a;
  vector<int> b;

  for (int i = 0; i < 4; i++) {a.push_back (i*2); b.push_back ((i+1)*3); }

  vector<int> c(4);	// allocate memory for 4 int values!!

  // use the algo "transform" and the function object "plus"
  // transfrom takes the elements of vectors a and b, adds them using
  // plus and writes the results to c

  transform (a.begin(), a.end(), b.begin(), c.begin(), plus<int>() );

  copy (c.begin(), c.end(), ostream_iterator<int> (cout, " ") );
}

Back to index


Johannes Weidl (J.Weidl@infosys.tuwien.ac.at) - Apr 16, 1996