Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

주문 세트 및 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