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;
}