for_each man page on SunOS

Man page or keyword search:  
man Server   20652 pages
apropos Keyword Search (all sections)
Output format
SunOS logo
[printable version]

for_each(3C++)			       -			for_each(3C++)

Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

NAME
       for_each

	- Applies a function to each element in a range.

SYNOPSIS
       #include <algorithm>
       template <class InputIterator, class Function>
       void for_each(InputIterator first, InputIterator last,
	      Function f);

DESCRIPTION
       The  for_each  algorithm	 applies  function  f  to  all	members of the
       sequence in the range [first, last), where first and last are iterators
       that  define  the  sequence.  Since  this a non-mutating algorithm, the
       function f cannot make any modifications to the sequence,  but  it  can
       achieve	results through side effects (such as copying or printing). If
       f returns a result, the result is ignored.

COMPLEXITY
       The function f is applied exactly last - first times.

EXAMPLE
//
// for_each.cpp
//
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;

 // Function class that outputs its argument times x
template <;class Arg>
class out_times_x :  private unary_function<;Arg,void>
 {
  private:
     Arg multiplier;

  public:
     out_times_x(const Arg& x) : multiplier(x) { }
     void operator()(const Arg& x)
	 { cout << x * multiplier << " " << endl; }
 };

int main()
 {
  int sequence[5] = {1,2,3,4,5};

   // Set up a vector
  vector<int> v(sequence,sequence + 5);

   // Setup a function object
  out_times_x<int> f2(2);

   for_each(v.begin(),v.end(),f2);   // Apply function

  return 0;
 }

Program Output

2
4
6
8
10

WARNINGS
       If your compiler does not support default template parameters, then you
       always  need  to	 supply the Allocator template argument. For instance,
       you have to write:

       vector<int, allocator<int> >

       instead of:

       vector<int>

       If your compiler does not support namespaces, then you do not need  the
       using declaration for std.

SEE ALSO
       Algorithms, Function_Objects

Rogue Wave Software		  02 Apr 1998			for_each(3C++)
[top]

List of man pages available for SunOS

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net