Computer >> Máy Tính >  >> Lập trình >> C ++

order_of_key () trong C ++

Trong hướng dẫn này, chúng ta sẽ thảo luận về một chương trình để hiểu order_of_key () trong C ++.

Hàm order_of_key () nhận một khóa và trả về số phần tử ít hơn khóa được cung cấp dưới dạng tham số trong một tập hợp có thứ tự.

Ví dụ

#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;
}

Đầu ra

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