VOOZH about

URL: https://www.geeksforgeeks.org/cpp/policy-based-data-structures-g/

⇱ Policy based data structures in g++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Policy based data structures in g++

Last Updated : 21 Aug, 2025

Policy-Based Data Structures (PBDS) are advanced data structures provided by the GNU C++ STL extension. They include structures like ordered sets and maps with extra features - allowing operations like finding the k-th smallest element or the number of elements less than a given value in O(log n ) time.

Headers required for using PBDS

The two most used PBDS containers are:

1. Ordered Set

2. Ordered Map

Operations in PBDS containers

Insertion

It adds the element to the container if it doesn't already exist.
PBDS maintains elements in sorted order automatically.


Output
10 20 30 

Deletion

erase() removes element form the container if it exists.
No error occurs if the element is not present.


Output
10 30 

Find operation

Used to check whether an element is present.


Output
20 is found

Lower bound

lower_bound(x) returns iterator to the first element ≥ x.
Similar to lower_bound in std::set.


Output
20

Upper Bound

upper_bound(x) returns iterator to the first element >x.
Useful in range queries.


Output
30

Size

It returns the number of elements in the container.


Output
Size: 3

Find by order

find_by_order(k) returns iterator to the k-th smallest element (0-indexed).
This is not available in std::set.


Output
Size: 3

Order of key

order_of_key(x) returns the number of elements strictly less than x.
Great for rank-based queries.

Advantages of PBDS

  • Allows finding the k-th smallest element quickly.
  • Counts how many elements are smaller than a given value fast.
  • Keeps data sorted with efficient insert and delete operations.
  • More powerful than normal sets and maps in C++.



Comment
Article Tags:
Article Tags: