trying to use transform to subtract 1 from a number
Hello,
I have the following code, IndexLower is a vector<int>:
transform(IndexLower.begin(), IndexLower.end(), IndexLower.begin(), bind1st(minus<int>(),1));
I want to subtract 1 form each member of the vector and store it back in the same vector, but with the following input:
58, 60
I get:
-57, -59
Why are the results negative instead of positive?
Thanks!
[441 byte] By [
lab1] at [2007-11-20 11:25:31]

# 1 Re: trying to use transform to subtract 1 from a number
Because you're binding 1 to the first argument of minus.
1 - 58 = -57
1 - 60 = -59
Here's what you want:
transform(IndexLower.begin(), IndexLower.end(), IndexLower.begin(), bind2nd(minus<int>(),1));
# 2 Re: trying to use transform to subtract 1 from a number
Use tr1::bind or boost::bind instead of bind1st/bind2nd. They are more extensible and bug free. You don't need to worry about a case-to-case basis solution and use them uniformly in your code.