DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

generate(3C++)


generate -- apply a given function to every location in an array

Synopsis

   template <class T>  
   void generate(void (*fun)(ptrdiff_t n,T* p),T* b,T* e);

Assumptions

None.

Description

For each cell in an array from the leftmost to the rightmost, generate calls function fun with two arguments: (1) the cell index (assuming that the index of the first cell is 0) and (2) a pointer to the cell.

Complexity

If N is the size of the array, then complexity is O(N). Exactly N function calls to fun are made.

Example

In the following example, iota assigns a consecutive integer starting from 0 to every location in the array (similar to the APL function iota):

       void zap(ptrdiff_t n, int* address){
           *address = n;
       }
       void iota(int* b, int* e){
           generate(zap, b, e);
       }

Notes

Because a Block (see Block(3C++)) can always be used wherever an array is called for, Array Algorithms can also be used with Blocks. In fact, these two components were actually designed to be used together.

References

Array_alg(3C++), Block(3C++), fill(3C++), for_each(3C++), subs(3C++)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004