C++ 中的 order_of_key()


本教程将讨论一个程序来理解 C++ 中的 order_of_key()。

order_of_key() 函数接受一个键,并返回一个有序集中小于作为参数提供的键的元素数。

示例

 在线演示

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

输出

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

更新日期:2020 年 4 月 6 日

612 次浏览

启动您的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.