g++ 中基于策略的数据结构
g++ 编译器是 GNU Linux 中的 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
这些数据结构非常通用,你可以执行许多功能,例如检查元素索引、查找索引中的元素等。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP