Studying STL, I wrote a simple program for testing functors and modifiers. My question is the difference using CLASS or STRUCT to write a functor and try to work with it using functional adapters. As far as I understand in C ++, the difference between CLASS and STRUCT is that in the latter case, members are public by default. This is also what I read many times in the answers on this site. Therefore, please explain to me why this short piece of code does not compile even if I declared all members (only overload functions ()) public when I try to use the not2 modifier. (I have not tried other modifiers, for example, binders)
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <class T>
void print (T i) {
cout << " " << i;
}
template <class T>
class mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
template <class T>
class generatore
{
public:
generatore (T start = 0, T stp = 1) : current(start), step(stp)
{ }
T operator() () { return current+=step; }
private:
T current;
T step;
};
int main () {
vector<int> first(10);
generate(first.begin(), first.end(), generatore<int>(10,10) );
first.resize(first.size()*2);
generate(first.begin()+first.size()/2, first.end(), generatore<int>(1,17) );
cout << "\nfirst :";
for_each (first.begin(), first.end(), print<int>);
cout << "\nFORWARD SORT :";
sort(first.begin(),first.end(),mystruct<int>());
for_each (first.begin(), first.end(), print<int>);
sort(first.begin(),first.end(),not2(mystruct<int>()));
cout << "\nBACKWARD SORT :";
for_each (first.begin(), first.end(), print<int>);
cout << endl;
}
Everithing , , :
struct mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
:
g++ struct.cpp
/usr/include/c++/4.2.1/bits/stl_function.h:
'Std:: binary_negate > :
struct.cpp: 52:
/usr/include/c++/4.2.1/bits/stl_function.h:116:
error: 'typedef int std:: binary_function:: first_argument_type -
/usr/include/c++/4.2.1/bits/stl_function.h:338:
:
/usr/include/c++/4.2.1/bits/stl_function.h:119:
error: 'typedef int std:: binary_function:: second_argument_type - ....
, , ?