could you tell me why this code works? There is overloaded operator() which is used by replace_if
algorithm. In main function I've created constant object of IsEqual class so only constant function member should be used. Somehow constancy isn't working and that operator is called.
#include <iostream>
#include <vector>
#include <algorithm>
class IsEqual {
int value;
public:
IsEqual(int v) : value(v) {}
bool operator()(const int &elem){
this->value=6;
return elem == value;
}
};
int main()
{
const IsEqual tst(2);
std::vector<int> vec = {3,2,1,4,3,7,8,6};
std::replace_if(vec.begin(), vec.end(), tst, 5);
for (int i : vec) std::cout << i << " ";
std::cout<<std::endl;
}
result: 3 2 1 4 3 7 8 5