views:

101

answers:

3

Under these conditions,

  1. The o/p of the first program is an large array of either integers, doubles or strings.
  2. Best method means the fastest on x86 architecture.

    • o/p means output. Sorry for being unclear.
A: 

If you do mean output then just pass the array straight into the next function with an integer saying how many elements there are. It will pass the address of the first element across (4 bytes on a 32-bit system or 8 bytes on a 64-bit system) and then pass the size across in 4 bytes.

If you are lucky the compiler will even pass these parameters via registers.

Goz
+1  A: 

If you're talking about two programs that run separatly you can use A pipe object.
In windows you would use `CreateNamedPipe()'
http://msdn.microsoft.com/en-us/library/aa365150%28VS.85%29.aspx

shoosh
Thanks shoosh..
Dilawar
Thanks are usually expressed as upvotes in this site.
shoosh
I don't have minimum of 15 reputation :-) ..
Dilawar
+1  A: 

You could give the 'other' function to the first function to call back onto:

#include <algorithm>
#include <iostream>

template<typename It, typename It2, typename F1, typename F2> 
void combine( It from, It to, It2 out, F1 f1, F2 f2 ) {
    for( int* p = from; p != to; ++p ) {
       *(out++) =  f2( f1( *p ) );
    }
}

int increment( int i ){ return ++i; }

int twice( int i ){ return i+i; }

int main() {
 int ints[]={1,2,3,4};
 int result[4];

 combine( ints, ints+4, result, increment, twice );
 std::copy( result, result+4, std::ostream_iterator<int>( std::cout, "; " ) );

}

Actually, this mechanism can be extended by pouring the functions into 'first class' objects. The STL has constructs to achieve this: if you wrap your free functions in a ptr_fun object, you can construct a better combine functor. In SGI's STL implementation, the compose function is available.

#include <functional>


template<typename F1, typename F2>
struct Combined : public std::unary_function
                         < typename F1::argument_type
                         , typename F2::result_type > {

    typedef typename F2::result_type result_type;
    typedef typename F1::argument_type argument_type;

    Combined( F1 f1, F2 f2 ): f1_( f1 ), f2_( f2 ) {}

    result_type operator()( argument_type arg ) const {
        return f2_( f1_( arg ) );
    }

private:
    F1 f1_;
    F2 f2_;
};

template<typename F1, typename F2>
Combined<F1,F2> combined( F1 f1, F2 f2 ) { return Combined<F1,F2>(f1,f2); }

And then use this functionality to combine functions even more generically:

#include <iostream>
#include <iterator>
#include <algorithm>

int increment(int i){ return ++i; }
int twice(int i) { return 2*i; }

int main() {
    using namespace std;
    int values[]={1,2,3,4};

    transform( values, values+4, ostream_iterator<int>( cout, "; " ), 
               combined( ptr_fun( increment ), ptr_fun( twice ) )
        );
    transform( values, values+4, ostream_iterator<int>( cout, "; " ), 
               combined( ptr_fun( increment ), 
                         combined( ptr_fun( increment ), ptr_fun( twice ) ) )
        );

}
xtofl
Thanks Dude, this is new!! :-)
Dilawar