Pytanie za 100 pkt: Dlaczego to się kompiluje (gcc/Ubuntu):
// list::remove_if
#include <iostream>
#include <list>
using namespace std;
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39 <=== ta linijka jest ok
cout << "mylist contains:";
for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
ale gdy zamiast prostego typu użyje klasy (tutaj Point) to już nie :
#include<iostream>
#include<algorithm>
#include<list>
using namespace std;
class Point
{
int x,y;
public:
Point(int a,int b)
{
x = a; y = b;
}
int getx() const
{
return x;
}
int gety() const
{
return y;
}
};
class Sth
{
list<Point> otoczenie;
bool x_rozne_od_zera(const Point &p)
{
return (p.getx() != 0);
}
public:
Sth(Point a,Point b)
{
otoczenie.clear();
otoczenie.insert(otoczenie.end(),a);
otoczenie.insert(otoczenie.end(),b);
otoczenie.remove_if(x_rozne_od_zera); // <==== ale to nie przechodzi
}
};
int main()
{
Point p(0,0);
Point r(2,1);
Sth obiekt(p,r);
return 0;
}
Oczywiście ten kod nie ma zbyt wielkiego sensu,chodzi tylko o zastosowanie remove_if