Re: If you can help... (map::find and map::insert)
On Jun 25, 10:40 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
Ricardo <rbalbi...@gmail.com> kirjutas:
I have seen a lot of comments about this problem on the
lists and, even as I tried to implement the proper
corrections, I was really unable to do so... (or my problem
is somehow different...).
I have a map that uses a class as its key.. something like this:
struct SFlowId {
uint32_t ingressInterface;
uint16_t VLAN;
uint32_t sourceIP;
uint32_t destinationIP;
uint16_t sourcePort;
uint16_t destinationPort;
};
I defined a < function to compare those values....
// this is short cause the problem occurs both with 2, 3 or 4
parameters being compared (anything more than 1 causes my problem).
bool operator<(const SFlowId &left,const SFlowId &right) {
return (left.destinationIP < right.destinationIP) ||
(!(left.destinationIP < right.destinationIP) &&
(left.sourceIP <
right.sourceIP));
Note that you have a redundancy here: !(left.destinationIP <
right.destinationIP) is always true if the first part of the expression
is not (left.destinationIP < right.destinationIP).
This already hints
that you have written something meaningless here. A proper weak ordering
predicate would be:
bool operator<(const SFlowId &left,const SFlowId &right) {
return (left.destinationIP < right.destinationIP) ||
((left.destinationIP == right.destinationIP) &&
(left.sourceIP < right.sourceIP));
or
return (left.destinationIP < right.destinationIP
|| ( ! (right.destinationIP < left.destinationIP)
&& left.sourceIP < right.sourceIP ) ;
That way, you only need to use '<'.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34