"Rares Vernica" <(E-Mail Removed)> a écrit dans le message de news:

(E-Mail Removed)...

> Hello,

>

> I need help to replace the following code:

>

> int *idx; //...

> for (int i = 0; i < n; i++, ++out)

> out = in[idx[i]];

>

> with something like:

>

> int *idx; //...

> std::transform(idx, idx + n, out, ...);

>

> I think the operator for transform should look something like:

>

> __gnu_cxx::compose1(in[], *idx)

>

> So, I think, I need a function object to apply the [] operator to "in",

> given an int and another one to apply the * operator to each element in

> "idx".

>

> Thanks in advance,

> Rares Vernica

>
here is a generic way to do what you want using only stl stuff.

using namespace std;

// here is the functor, I assume idx is always of type int, if not, replace

it with a template parameter

template<typename T_out, typename T_in>

struct Get : public binary_function<int, T_in, T_out>

{

// binary_function is needed by bind2nd

T_out operator()(int idx, T_in in) const

{

return in[idx];

}

};

int main()

{

int idx[3] = {0,1,2};

int in[3] = {5,4,3};

int out[3];

transform(idx, idx+3, out, bind2nd(Get<int,int*>(), &in[0]));

cout << out[0] <<"\n" <<out[1] <<"\n" <<out[2] <<"\n";

return 0;

}

this print:

5

4

3

---------------------------

Eric Pruneau