order_of_key() in C++ Last Updated : 06 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 // 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, tree_order_statistics_node_update> ordered_set; // Driver code int main() { ordered_set mySet; // Inserting elements to ordered_set mySet.insert(5); mySet.insert(2); mySet.insert(6); mySet.insert(4); // 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; } Output: Count of elements less than 6::3 Count of elements less than 7 ::4 Comment More infoAdvertise with us Next Article order_of_key() in C++ J Jasbir Singh 3 Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Order of Evaluation in C++17 In C++ programming, the order of evaluation of expressions can have a significant impact on the behavior and correctness of the code. C++17 introduced changes to the order of evaluation rules, providing clearer guidelines and improving consistency across different compilers. In this article, we will 4 min read Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not 7 min read C++ Keywords Keywords are the reserved words that have special meanings in the C++ language. They are the words that have special meaning in the language. C++ uses keywords for a specifying the components of the language, such as void, int, public, etc. They can't be used for a variable name, function name or an 2 min read unordered_map size() in C++ STL The unordered_multimap::size() is a built-in function in C++ Standard Template Library which return's the number of element in the unordered map. Syntax: unordered_multimap_name.size() Return Value: It returns the number of the element present in the unordered map. Time complexity: Constant i.e. O(1 1 min read unordered_map at() in C++ In C++, unordered_map at() is a library function used to find the value associated with a given key in the unordered_map container. Let's look at an example to see how to use this function.C++#include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, string> um = {{1, 2 min read Like