The Wayback Machine - https://web.archive.org/web/20231217112837/https://www.geeksforgeeks.org/map-policy-based-data-structure-in-g/
Open In App
Related Articles

Map Policy Based Data Structure in g++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

There are some data structures that are supported by g++ compiler and are not a part of the C++ standard library. One of these is: Policy-Based Data Structure, which is used for high-performance, flexibility, semantic safety, and conformance to the corresponding containers in std. 
This can also be used as a map, to store key and a value (pair) in a sorted order. 
So to use this Data Structure, the following lines must be added to the code:
 

CPP




#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
 
using namespace __gnu_pbds;


Following is the code showing policy-based data structure as a map, it can add/remove pairs of elements, can find the number of pairs less than (x, y), kth smallest pair etc. in O(log N) time. The specialty of this map is that we have access to the indices that the pair of elements would have in a sorted 2D array. If the pair does not appear in the map, we get the position that the pair would have in the map.
 

CPP




// Program showing a Policy Based Data Structure as a map
#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;
 
// A new data structure is defined
// Please refer https : // goo.gl/WVDL6g
typedef tree<pair<int, int>, null_type, less<pair<int, int> >,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_map;
 
// Driver code
int main()
{
    ordered_map om;
 
    om.insert({ 23, 20 });
    om.insert({ 23, 10 });
    om.insert({ 23, 30 });
    om.insert({ 12, 35 });
    om.insert({ 12, 22 });
 
    // Another method to insert pair
    // om.insert(make_pair(23, 20));
 
    // Print the contents of the map
    cout << "Contents of map:\n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = om.begin(); itr != om.end(); ++itr) {
        cout << itr->first << "\t" << itr->second << "\n";
    }
 
    // value at 3rd index in sorted array.
    cout << "The value at 3rd index is "
         << "{" << om.find_by_order(3)->first << ", "
         << om.find_by_order(3)->second << "}\n";
 
    // Index of pair {23, 30}
    cout << "The index of pair {23, 30} is "
         << om.order_of_key({ 23, 30 }) << endl;
 
    // Pair {23, 40} is not in the map but it will show the
    // index number if it was there in sorted array
    cout << "The index of pair {23, 40} is "
         << om.order_of_key({ 23, 40 }) << endl;
 
    return 0;
}


Output: 

Contents of map:
KEY    ELEMENT
12    22
12    35
23    10
23    20
23    30
The value at 3rd index is {23, 20}
The index of pair {23, 30} is 4
The index of pair {23, 40} is 5

 


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 04 Sep, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials