
-----------------------------------
wtd
Tue Oct 31, 2006 4:30 pm

A Test
-----------------------------------
Without compiling the following code, predict its output.

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using __gnu_cxx::compose1;

int main()
{
   vector v;
   v.push_back(1);
   v.push_back(2);

   vector v2;

   transform(v.begin(), v.end(), back_inserter(v2),
             compose1(bind2nd(multiplies(), 2),
                      negate()));

   copy(v2.begin(), v2.end(), ostream_iterator(cout, "\n"));
}


-----------------------------------
r.3volved
Tue Oct 31, 2006 5:10 pm


-----------------------------------
not sure what this does exactly:
compose1( bind2nd( multiplies(), 2 ), negate() )

I imagine it does some calculation on the value stored in each element of v and applies the modified v element to v2 as it's calculated, then prints the vector of v2.

Is compose1 something you've created?
I'd hope it's not an STL naming convention.

-----------------------------------
wtd
Tue Oct 31, 2006 5:37 pm


-----------------------------------
The compose1 function is a convenient way of creating a UnaryCompose function object.  It is an SGI extension to the STL, but GCC includes it via the "ext/functional" header, and it resides in the __gnu_cxx namespace.

-----------------------------------
abcdefghijklmnopqrstuvwxy
Sun Jan 21, 2007 5:46 am

RE:A test
-----------------------------------
At first glance I had no idea what this thing could possibly output.  Then I thought it was a trick question cause I didn't see "cout" anywhere.  But then I found "cout"  and realized it does print something...  Then I started writing my thoughts in this post and I came to an answer, although not a confident one...


The compose1 function is a convenient way of creating a UnaryCompose function object.


So, in beginners terms, compose1 takes two functions and makes them into one?  If so, I doubt it just joins them together.  How does it work?


back_inserter(v2)
 

I looked up this function and found: "Creates an iterator that can insert elements at the back of a specified container."  So in this case won't it return v2.begin()?  


bind2nd(multiplies(), 2)


Normally I would scratch my head.  multiplies() takes no arguments so how does it know what to multiply by?  Then I noticed the "bind2nd" and I figure it means, bind the 2 to multiplies.  So I'm guessing this means multiply by 2.  Or at least it should if code has any purpose whatsoever... I mean why not code in binary if we are going to code obfuscatedly?


compose1(bind2nd(multiplies(), 2),
                      negate())); 


if both functions are carried out.  we've multiplied and then made each number negative.  

So the output is: 


-2
-4


-----------------------------------
ownageprince
Sun Jan 21, 2007 9:36 am

Re: A Test
-----------------------------------
v.push_back(1);
   v.push_back(2);

this the numbers by which 

transform(v.begin(), v.end(), back_inserter(v2),
             compose1(bind2nd(multiplies(), 2), 