Блог пользователя Halym2007

Автор Halym2007, история, 3 года назад, По-английски

both code did same.what is the difference?

First one : 
set <int> s;
s.insert (1);s.insert (3);s.insert (5);
auto tr = s.lower_bound (2);
s.erase (tr);<-----------here
--------------------------
Second one : 
set <int> s;
s.insert (1);s.insert (3);s.insert (5);
auto tr = s.lower_bound (2);
s.erase (*tr);<-----------here
  • Проголосовать: нравится
  • +40
  • Проголосовать: не нравится

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

Auto comment: topic has been updated by Halym2007 (previous revision, new revision, compare).

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

Auto comment: topic has been updated by Halym2007 (previous revision, new revision, compare).

»
3 года назад, скрыть # |
← Rev. 4  
Проголосовать: нравится -22 Проголосовать: не нравится

.

»
3 года назад, скрыть # |
← Rev. 3  
Проголосовать: нравится +68 Проголосовать: не нравится

The real difference is when you use multiset and have multiple values, the first code removes only one occurrence of 3(lower_bound of 2 in the set is 3) but the second one removes all of them.

The reason is that tr is a pointer to one element, while *tr is the value of the pointer, which when used in a multiset, removes all occurrences of that element.

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

tr is an iterator so is pointing to the memory address while *tr means dereferencing the iterator and is accessing value at that memory address

»
3 года назад, скрыть # |
 
Проголосовать: нравится +16 Проголосовать: не нравится

First One s.erase(tr) -> O(1)

Second One s.erase(*tr) -> O(logN)