Associative containter-Set is new to me and was used in my program, and I found there ARE duplicated members in the Set. I suspected this could be related to the way the call-back function is defined. The element type for Set is a struct pointT(with x and y as members). Can any expert here kindly reivew a snippet of the code and give some advice?
Hey, Thanks for your reply.
I am using a university package, which uses std::set under the hood. I just verified using simple element type(integer), and set does not allow duplicated members. :-(
Then it would seem there is a bug in the package. set does not allow duplicates. Note that you can define your own comparator function for set to do anything you want.
This really solved the problem! Thanks a lot.
While I still do not quite understand why this change can totally change the behavior of set, could you shed some light on this?
I was thinking internally the member duplication is only flagged on when a.col== b.col && a.row ==b.row. Why the second branch(else if) would matter?
Before it can be compared for equality using your first case, the element must be found, which involves the second case. If the ordering is not strict, it may not find an existing element after others have been added.
No sets are created when you create the queue, so you don't need any initialization of the sets at that point. Instead, initialize the sets when you create them before you push them onto the queue. So just do this:
Queue<Set<pointT> > q;
Then use it like this:
1 2 3
Set<pointT> s(CmpByRowCol);
// Insert some stuff into s, then:
q.push( s );
Of course, you may wish to push pointers onto the queue instead of (copies of) the sets.
Hmm, I tried this approach, however it has the same complain that the set in the queue does not have an appropriate comparison callback function when I enqueued the set.