Policy-based data structures in g++

C++Server Side ProgrammingProgramming

g++ compiler is a C++ compiler for GNU in Linux.

The g++ compiler also adds support for some special data structures that are not in C++ programming language standard library. These are known as policy-based data structures.

The policy-based data structures provide the programmer a high-performance, semantic safety and flexibility as compared to the standard data structures of C++ std library.

To include these data structures to your program, the following lines should be added,

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;


Let’s see a program to see how these policy-based data structures work.

 Live Demo

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
int main() {
   new_data_set data;
   cout<<"The value at index 2 is "<<*data.find_by_order(2)<<endl;
   cout<<"The index of number 87 is "<<data.order_of_key(87)<<endl;
   return 0;


The value at index 2 is 785
The index of number 87 is 4

These data structures are so versatile that you can go for many functions like checking index of element, finding elements at index, etc.

Updated on 17-Apr-2020 08:55:21