Policy based data structures in g++
The g++ compiler also supports some data structures that are not part of the C++ standard library. Such structures are called policy-based data structures. These data structures are designed for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std.
To use these structures, the following lines must be added to the code:
For example, following is a code showing a policy-based data structure that is like set, it can add/remove elements, can find the number of elements less than x, kth smallest element etc in O(logn) time. It can also be indexed like an array. The specialty of this set is that we have access to the indices that the elements would have in a sorted array. If the element does not appear in the set, we get the position that the element would have in the set.
The value at 3rd index ::6 The index of number 6::3 The index of number seven ::4
NOTE: Both the functions order_of_key and find_by_order work in logarithmic time.
NOTE (FOR WINDOW USERS) : Window users might face problem importing pbds library . To fix it follow the following steps:
Step 1: Go to directory where MinGW is installed.
Step 2: Navigate to \lib\gcc\mingw32\8.2.0\include\c++\ext\pb_ds\detail\resize_policy
Step 3: Rename “hash_standard_resize_policy_imp.hpp0000644” to “hash_standard_resize_policy_imp.hpp”
In order to insert multiple copies of the same element in the ordered set replace the following line,
typedef tree<int , null_type, less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;
typedef tree<int , null_type , less_equal<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_multiset
The element present at the index 0 is 2 The element present at the index 1 is 2 The element present at the index 2 is 4 The element present at the index 3 is 5 The element present at the index 4 is 5 The element present at the index 5 is 5 The element present at the index 6 is 6