order_of_key() in C++

The order_of_key() is a builtin function of Ordered Set which is a Policy Based Data Structure in C++. Policy-based data structures are not part of the C++ standard library but g++ compiler supports them.

Ordered set is a policy based data structure in g++ that keeps the unique elements in sorted order. It performs all the operations as performed by the set data structure in STL in log(n) complexity and performs two additional operations also in log(n) complexity .

Two important operations that we can perform in these data structures:

  • order_of_key: The number of items in a set that are strictly smaller than k
  • find_by_order: It returns an iterator to the ith largest element

The order_of_key() function accepts a key and returns the number of elements in the ordered set which are strictly less than the key passed to it as a parameter.

For example,

Let us assume we have a set s : {1, 5, 6, 17, 88}, then :
s.order_of_key(6) : Count of elements strictly smaller than 6 is 2.
s.order_of_key(25) : Count of elements strictly smaller than 25 is 4.

Comparison with lower_bound() and distance()

  • In C++ we have std::distance which takes two iterators and calculates the distance between them. i.e The number of elements between them. It can be an alternative for find_by_order but it’s time complexity is O(n) for an ordered set.
  • lower_bound is also an option and it takes log(n) amount of time. It returns an iterator to the first element which is not less than k. But since it returns an iterator we can never know the index or number of elements which are smaller than k.

    For vectors and linear data structures, one can perform :

    index = lower_bound(k) – myvector.begin() ;

    But since set is a tree based data structure, we cannot perform this operation.

Below program illustrate the implementation of order_of_key():





// CPP program to illustrate order_of_key()
// for policy based data structures
#include <iostream> 
using namespace std; 
// Important header files  
#include <ext/pb_ds/assoc_container.hpp> // Common file 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
#include <iostream> 
using namespace __gnu_pbds; 
using namespace std; 
// Declaring ordered_set
typedef tree<int, null_type, less<int>, rb_tree_tag, 
// Driver code 
int main() 
    ordered_set mySet;
    // Inserting elements to ordered_set
    // count of elements less than 6 
    cout << "Count of elements less than 6::"
        << mySet.order_of_key(6) << endl; 
    // number 7 not in the set but it will show the 
    // index number if it was there in sorted array. 
    cout << "Count of elements less than 7 ::"
        << mySet.order_of_key(7) << endl; 
    return 0; 



Count of elements less than 6::3
Count of elements less than 7 ::4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.