有序集和 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