g++ compiler เป็นคอมไพเลอร์ C++ สำหรับ GNU ใน Linux
คอมไพเลอร์ g++ ยังเพิ่มการรองรับโครงสร้างข้อมูลพิเศษบางอย่างที่ไม่อยู่ในไลบรารีมาตรฐานของภาษา C++ โครงสร้างเหล่านี้เรียกว่า policy-based data structures
Policy-based data structures ให้โปรแกรมเมอร์ประสิทธิภาพสูง ความปลอดภัยทางความหมาย (semantic safety) และความยืดหยุ่นมากกว่าโครงสร้างข้อมูลมาตรฐานของ C++ std library
เพื่อรวมโครงสร้างข้อมูลเหล่านี้ในโปรแกรมของคุณ บรรทัดต่อไปนี้ควรถูกเพิ่ม:
#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
ตัวอย่าง
มาดูโปรแกรมเพื่อดูว่า policy-based data structures นี้ทำงานอย่างไร:
#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;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
int main() {
new_data_set data;
data.insert(34);
data.insert(785);
data.insert(12);
data.insert(87);
cout << "The value at index 2 is " << *data.find_by_order(2) << endl;
cout << "The index of number 87 is " << data.order_of_key(87) << endl;
return 0;
}
ผลลัพธ์
The value at index 2 is 785 The index of number 87 is 4
โครงสร้างข้อมูลเหล่านี้หลากหลายมาก คุณสามารถใช้ฟังก์ชันมากมาย เช่น ตรวจสอบดัชนีขององค์ประกอบ ค้นหาองค์ประกอบที่ดัชนี ฯลฯ