Order Statistic Tree in C++

Here is the example of GNU Policy-Based STL MAP implemented as order statistic tree (tested on Linux gcc 4.6.1): #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef tree< int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> map_t; int main() { map_t s; s.insert(make_pair(12, 1012)); s.insert(make_pair(505, 1505)); s.insert(make_pair(30, 1030)); cout << s.find_by_order(1)->second << ‘\n’; … Read more

Insert into an STL queue using std::copy

Unfortunately std::queue ‘adapts’ the function known as push_back to just push which means that the standard back_insert_iterator doesn’t work. Probably the simplest way (albeit conceptually ugly) is to adapt the container adapter with a short lived container adapter adapter[sic] (eugh!) that lives as long as the back insert iterator. template<class T> class QueueAdapter { public: … Read more

STL algorithms: Why no additional interface for containers (additional to iterator pairs)?

They do introduce ambiguity for many algorithms. A lot of <algorithm> looks like template<class iterator> void do_something(iterator, iterator); template<class iterator, class funct> void do_something(iterator, iterator, funct); If you add additional overloads template<class container, class funct> void do_something(container, funct); the compiler will have some trouble figuring out what do_something(x, y) means. If x and y are … Read more