Hi All,
I am trying to learn stl to have some knowledge of how things work.
I have just covered some basics and I have come to a block.

I will explain what I am trying to do.
I have a class that performs some basic operations on its member. This class is a wrapper for an integer, just to keep it simple with some member functions.

I create a vector of this class and have five values stored.
What I want to do is learn bind2nd, I want to fix the second parameter for multiples stl function. This gives compile errors.

Please help. I have tried searching the internet and I could not find anything similar to what I am doing.

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <map>
#include <utility>
#include <vector>
#include <numeric>
#include <functional>

class standard
      int i;
             friend std::ostream &operator<<(std::ostream &out, const standard &rhs);
             standard(int x):i(x){}
             standard (const standard &std)
             int operator==(const standard &rhs) const
                  return (rhs.getval()==i);
             int operator>(const standard &rhs) const
                  return (i>rhs.getval());
             int operator<(const standard &rhs) const
                  return (i<rhs.getval());
             const int getval() const
                 return i;
             const standard operator*(const standard &rhs) const
                   return standard(i*rhs.getval());
             const standard operator+(const standard &rhs) const
                   return standard(i+rhs.getval());
std::ostream &operator<<(std::ostream &output, const standard &rhs)
   output << rhs.getval();
   return output;

int main()
    std::vector<standard> a1;
    std::vector<standard> a2;

           i want to multiply all the values ie 1*1*2*3*4*5 
standard t=accumulate (a1.begin(), a1.end(), standard(1), std::multiplies<standard>()); 

i want to multiply all the values by 2
standard r=accumulate (a1.begin(), a1.end(), standard(1),
std::cout << "\n mult = " <<r.getval();               

    return 1;

Edited 6 Years Ago by Nick Evan: Changed icode-tags to code-tags for syntax highlighting

I think that your test case here is ill advised. Here is why... The accumulate function is expecting a binary function as its fourth parameter. bind2nd returns a unary function (that is its purpose) by binding a value with the second parameter of a binary function. This allows use of binary functions in situations where unary functions are expected.

This is indicated by error message...

term does not evaluate to a function taking 2 arguments

this indicates to us, exactly as it says, that the accumulate function is expecting an argument which evaluates to a function taking 2 arguments. Something that you are not currently providing.

Rewrite your test case chosing a generic function which has a unary function as its argument, for example, find_if (maybe search a vector for values greater than an arbitrary value). Now you can use bind2nd to convert a binary function i.e. a comparison of two values to a unary.

This article has been dead for over six months. Start a new discussion instead.