The Simple Functional Template Library
[[expand]]
 

expand

Definition

C<X> expand( C<X> container, F<C,X,Y> functor );

where :

  • C is a container type (template)
  • F is the functor type (template)
  • X is the type of objects in the input container

Parameters

Parameter Description
container an STL container of type C holding objects type X
functor a functor taking a parameter of type X and returning a container of type C with objects of type Y

Return value

A container of type C holding objects of type X

Description

Calls the functor for each element in the input container and merges the resulting collections into a single one.

Examples

// source : examples/expand_example.cpp
#include <iostream>
#include <list>
#include <algorithm>
#include "sftl.h"
 
using namespace std;
 
// collapses a number into digits
 
list<short> get_digits( int x ) {
    list<short> digits;
    while (x>0) {
        digits.push_front( x % 10 );
        x/=10;
    }
    return digits;
}
 
void print( short x ) { cout << x << endl; }
 
int main() {
 
    // create vector of numbers
    int numbers [] = {101,24,55,71};
    list<int> v_numbers( &numbers[0], &numbers[4] );
 
    // obtain all digits
    list<short> all_digits = expand( v_numbers, get_digits );
 
    // print them
    apply( all_digits, print ); // prints 1 0 1 2 4 5 5 7 1
}
 
expand.txt · Last modified: 2007/08/24 23:31 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki