set iterator is not like vector iterator i cant add to the iteratire so how can i do binary search on a set and how can i find the diffrence betwen two positions in a set
set iterator is not like vector iterator i cant add to the iteratire so how can i do binary search on a set and how can i find the diffrence betwen two positions in a set
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3757 |
2 | jiangly | 3647 |
3 | Benq | 3581 |
4 | orzdevinwang | 3570 |
5 | Geothermal | 3569 |
5 | cnnfls_csy | 3569 |
7 | Radewoosh | 3509 |
8 | ecnerwala | 3486 |
9 | jqdai0815 | 3474 |
10 | gyh20 | 3447 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | maomao90 | 171 |
2 | awoo | 165 |
3 | adamant | 163 |
4 | TheScrasse | 159 |
5 | maroonrk | 155 |
6 | nor | 154 |
7 | -is-this-fft- | 152 |
8 | Petr | 147 |
9 | orz | 146 |
10 | pajenegod | 145 |
Название |
---|
Auto comment: topic has been updated by Aly (previous revision, new revision, compare).
You can use :
s.lower_bound(x)
s.upper_bound(x)
thanks , but i want to find the index of the returned iterator from the function
It's impossible ! :(
But you can use Ordered_Set
ref: https://www.geeksforgeeks.org/ordered-set-gnu-c-pbds/
Sounds like you need a self-balancing binary search tree. You can learn something about __gnu_pbds :: tree, which has implemented one.
thanks :)
index can be find using distance of required iterator with the begin iterator, distance is present in c++ stl.
That is O(n) if you use a set (or multiset, for that matter), since iterators work differently in those data structures. The reason it works in O(1) for a vector is that vectors have random-access iterators.
thanks :)
lower_bound(k) //O(n)
s.lower_bound(k) //O(log n)
thanks :)
You can solve pbds if you are facing problem due to limitations in set and multiset. You can learn it from here: https://www.geeksforgeeks.org/ordered-set-gnu-c-pbds/amp/
Make sure you are using c++20, because pbds use red-black tree and it applies operations in log(n) but it consumes slidely more time than set and multiset.C++20 is faster. So it should be preferred. You can see the blog to realise that: https://codeforces.com/blog/entry/113537
use ordered_set with s.order_of_key(x) which gives the position of x in log(n) time.