There is an associated container in C++ which is actually a set (multiset) which can give the order of an element in it in.
Here is how I use the container:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;
The problem is, I am not able to erase an element from it:
ordered_multiset<int> s;
s.insert(0);
s.erase(0);
cout << s.order_of_key(1); // returns number of elements less than x
// Outputs: 1
The strange part is that if you replace less_equal
with less
, then you would be able to do the job without a problem, actually if you use the container as a multiset, then you won't be able to erase elements from it, but there is no problem when you use it as a set.
- What is causing the problem?
- How can I solve the problem?
Note: Please don't suggest solving the problem in way of using another container. That's not a solution.
std::multiset
? – Kumasiorder_of_key
. In multiset doing this job is O(n). – Joubertstd::less_equal
whenstd::less
is expected (it's the default)? – Kumasistd::less_equal
to fake a multiset? That doesn't sound safe – Kumasiusing namespace std;using namespace __gnu_pbds;
, be explicit about the namespace your are talking about.ordered_multiset
is not part of c++, but of some non-common extension – Matricesordered_multiset
is defined on the first code. – JoubertThe problem is, I am not able to erase an element from it
. People cannot answer this question if they can't find the reference. – Matricestree
– Juarezusing ordered_multiset = tree< ...
on my first code in the question? – Joubertorder_of_key
in O(1). – JoubertO(1)
comes from fororder_of_key
, but I am very suspicious of it, given that lookup isO(log(n))
– Juarezstd::multiset
. – Joubertorder_of_key
, it's logarithmic. – Joubert