On 11 sep, 03:24, suresh <suresh.amritap...@gmail.com> wrote:
Hi,
In my code, the operator() method of a function object is designed t=
fill a set which is a private data member of the function object. This
function object is used in min_element algorithm.
Since the min_element algorithm uses a copy of the function object (as
per my understanding - hoping it to be true), I am unable to access
the set data member of the function object. A minimal code is show
below:
class FO{
public:
bool operator()(int a, int b);
private:
set<int> s;};
bool FO::operator()(int a,int b){
s.insert(a);
return (a<b);}
vector<int> v;
//vector populated
FO fo;
min_element(v.begin(),v.end(),fo);
Only after some time I realised that min_element takes only a copy of
the variable fo. [I found that the size of the set is always zero
after the call to min_element]. As a solution to this problem, I
tried to define the variable s as a pointer to set (set<int> *s) and
then modified the code accordingly but when the code is running, half
way thru, the code aborts saying corrupted double-linked list:
0x000000000102beb0 ***.
What is the solution to this issue?
My crystal ball suggests that you delete 's' member in the destructor
causing it to be corrupted in copies of fo.
Try something like:
class FO{
public:
FO( set<int>& is):s(&is){}
bool operator()(int a, int b);
private:
set<int>* s;
};
bool FO::operator()(int a,int b){
s->insert(a);
return (a<b);
}
set<int> fo;
min_element(v.begin(),v.end(),FO(fo));
looking for a different solution.