order_of_key() in C++


In this tutorial, we will be discussing a program to understand order_of_key() in C++.

The function order_of_key() takes in a key and returns the number of elements which are less than the key provided as parameter in an ordered set.

Example

 Live Demo

#include <iostream>
using namespace std;
#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;
//initializing ordered set
typedef tree<int, null_type, less<int>, rb_tree_tag,
      tree_order_statistics_node_update>
   ordered_set;
int main(){
   ordered_set mySet;
   mySet.insert(5);
   mySet.insert(2);
   mySet.insert(6);
   mySet.insert(4);
      cout << "Count of elements less than 6::"<< mySet.order_of_key(6) << endl;
      cout << "Count of elements less than 7 ::"<< mySet.order_of_key(7) << endl;
   return 0;
}

Output

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

Updated on: 06-Apr-2020

539 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements