G ++中基于策略的数据结构
g++编译器是Linux中GNU的C++编译器。
g++编译器还增加了对某些不在C++编程语言标准库中的特殊数据结构的支持。这些被称为基于策略的数据结构。
与C++std库的标准数据结构相比,基于策略的数据结构为程序员提供了高性能,语义安全性和灵活性。
为了将这些数据结构包括到您的程序中,应添加以下几行,
#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds;
示例
让我们看一个程序,看看这些基于策略的数据结构如何工作。
#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
这些数据结构用途广泛,您可以使用许多功能,例如检查元素的索引,在索引处查找元素等。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短