0

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
{
      private:
      int i;
      public:
             friend std::ostream &operator<<(std::ostream &out, const standard &rhs);
             standard()
             {
                       i=0;
             }
             standard(int x):i(x){}
             
             standard (const standard &std)
             {
                      i=std.getval();
             }
             
             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;
    
    a1.reserve(5);
    a2.reserve(5);
    a1.push_back(standard(1));
    a1.push_back(standard(2));
    a1.push_back(standard(3));
    a1.push_back(standard(4));
    a1.push_back(standard(5));

/*
           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::bind2nd(std::multiplies<standard>(),standard(2)));                              
std::cout << "\n mult = " <<r.getval();               
    

    return 1;
}

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

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by mattjbond
0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.