有序集和 GNU C++ PBDS
在本教程中,我们将讨论一个程序,以了解有序集合和 GNU C++ PBDS。
有序集合是一种基于策略的结构,它不同于 STL 库中的集合。有序集合将所有元素按排序顺序进行排列,并且不允许重复值。
示例
#include <iostream>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update>
int main(){
//declaring ordered set
ordered_set o_set;
o_set.insert(5);
o_set.insert(1);
o_set.insert(2);
cout << *(o_set.find_by_order(1))
<< endl;
cout << o_set.order_of_key(4)
<< endl;
cout << o_set.order_of_key(5)
<< endl;
if (o_set.find(2) != o_set.end())
o_set.erase(o_set.find(2));
cout << *(o_set.find_by_order(1))
<< endl;
cout << o_set.order_of_key(4)
<< endl;
return 0;
}输出
2 2 2 5 1
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP