Secret.Codes's blog

By Secret.Codes, 7 years ago, In English

I created a set of structure and sorted was manually. See the code...


class edge { public: int u, v, w;//variables on set edge() {} edge(int a, int b,int c) { u = a; v = b; w = c; } bool operator < ( const edge& z ) const { return w < z.w;//compare as your wish } }; set<edge> e;

Then tried to insert some element as u v w

1 2 10

1 3 8

3 2 3

1 4 3

1 3 6

2 1 2

But 1 4 3 didn't added. Then i used multiset and it worked . why?? we know set doesn't contain duplicate element. But there are no duplicate.

  • Vote: I like it
  • -15
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +9 Vote: I do not like it

Well, your implementation of set thinks that different elements are the ones that differ only by w. And you already have an element with w = 3 in your set.

»
7 years ago, # |
  Vote: I like it +9 Vote: I do not like it

But there are no duplicate.

Oh yes, there are.

Take a look at your comparator:

bool operator < ( const edge& z ) const {
    return w < z.w;//compare as your wish 
}

As you can see, you take into account only the w value. So any two edges having the same w value are considered equal.