Re: Is it possible to insert multiple pointer to pointers in std::set<node **, iterator_pointer> ?
Am 06.07.2011 20:20, schrieb allswellthatendswell:
We created a std:::set<node **, iterator_pointer> where node is
defined by this class: class node{
public:
Range value;
//value stored in the node
node *next;
//pointer to next node
node *prev;
//pointer to previous node
node(){ next = NULL; prev = NULL; }
node(Range r){ value = r; next = NULL; prev = NULL; }
~node();
};
and iterator_pointer is defined by the struct: struct iterator_pointer
{ bool operator()(node** r1, node** r2) const{
if (*r1 == 0 || *r2 == 0){
return false;
}
return ((*r1)->value.high()< (*r2)->value.high());
}
For some unknown reason when we only insert one pointer to pointer
(node**) into the std::set<node**, iterator_pointer>. When we try
inserting multiple unique pointer to pointers(node __), the size of
the std:set<node**, iterator_pointer> stays the same.
We tried stepping into STL template code but we couldn't
determine why the size of std:Set does not continue increasing for
each successive unique node** insert. Are we doing something wrong or
is not possible to insert multiple unique keys correctly. Thank you.
Yes, you are doing something wrong: Your ordering criterion
iterator_pointer does not satisfy the requirements of a strict weak
ordering. In particular the first test
if (*r1 == 0 || *r2 == 0){
return false;
}
is clearly defect, because it ensures that - given an object comp of
that type and an object n of type node** such that *n == 0 - both
comp(x, n) == false
comp(n, x) == false
evaluate to false, thus having the effect that x and n are equivalent
for *every* pointer value x. The obvious fix is to define a proper
ordering between null pointee and non-null pointee values, e.g.
bool operator()(node** r1, node** r2) const {
if (*r1 == 0){
return (*r2 != 0);
} else if (*r2 == 0) {
return false;
}
return ((*r1)->value.high() < (*r2)->value.high());
}
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]