Блог пользователя Secret.Codes

Автор Secret.Codes, 7 лет назад, По-английски

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.

  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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 лет назад, # |
  Проголосовать: нравится +9 Проголосовать: не нравится

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.