📜  在 g++ 中映射基于策略的数据结构

📅  最后修改于: 2022-05-13 01:55:39.574000             🧑  作者: Mango

在 g++ 中映射基于策略的数据结构

有一些数据结构受 g++ 编译器支持,而不是 C++ 标准库的一部分。其中之一是:基于策略的数据结构,用于高性能、灵活性、语义安全以及与 std 中相应容器的一致性。
这也可以用作映射,以按排序顺序存储键和值(对)。
所以要使用这个数据结构,必须在代码中添加以下几行:

CPP
#include  // Common file
#include 
#include  // for less
 
using namespace __gnu_pbds;


CPP
// Program showing a Policy Based Data Structure as a map
#include 
#include 
#include 
#include 
using namespace __gnu_pbds;
using namespace std;
 
// A new data structure is defined
// Please refer https : // goo.gl/WVDL6g
typedef tree, null_type, less >,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_map;
 
// Driver code
int main()
{
    ordered_map om;
 
    om.insert({ 23, 20 });
    om.insert({ 23, 10 });
    om.insert({ 23, 30 });
    om.insert({ 12, 35 });
    om.insert({ 12, 22 });
 
    // Another method to insert pair
    // om.insert(make_pair(23, 20));
 
    // Print the contents of the map
    cout << "Contents of map:\n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = om.begin(); itr != om.end(); ++itr) {
        cout << itr->first << "\t" << itr->second << "\n";
    }
 
    // value at 3rd index in sorted array.
    cout << "The value at 3rd index is "
         << "{" << om.find_by_order(3)->first << ", "
         << om.find_by_order(3)->second << "}\n";
 
    // Index of pair {23, 30}
    cout << "The index of pair {23, 30} is "
         << om.order_of_key({ 23, 30 }) << endl;
 
    // Pair {23, 40} is not in the map but it will show the
    // index number if it was there in sorted array
    cout << "The index of pair {23, 40} is "
         << om.order_of_key({ 23, 40 }) << endl;
 
    return 0;
}


以下是将基于策略的数据结构显示为映射的代码,它可以添加/删除元素对,可以在 O(log N) 时间内找到小于 (x, y)、第 k 个最小对等的对数。此映射的特殊之处在于我们可以访问这对元素在已排序的 2D 数组中的索引。如果该对未出现在地图中,我们将获得该对在地图中的位置。

CPP

// Program showing a Policy Based Data Structure as a map
#include 
#include 
#include 
#include 
using namespace __gnu_pbds;
using namespace std;
 
// A new data structure is defined
// Please refer https : // goo.gl/WVDL6g
typedef tree, null_type, less >,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_map;
 
// Driver code
int main()
{
    ordered_map om;
 
    om.insert({ 23, 20 });
    om.insert({ 23, 10 });
    om.insert({ 23, 30 });
    om.insert({ 12, 35 });
    om.insert({ 12, 22 });
 
    // Another method to insert pair
    // om.insert(make_pair(23, 20));
 
    // Print the contents of the map
    cout << "Contents of map:\n";
    cout << "KEY\tELEMENT\n";
    for (auto itr = om.begin(); itr != om.end(); ++itr) {
        cout << itr->first << "\t" << itr->second << "\n";
    }
 
    // value at 3rd index in sorted array.
    cout << "The value at 3rd index is "
         << "{" << om.find_by_order(3)->first << ", "
         << om.find_by_order(3)->second << "}\n";
 
    // Index of pair {23, 30}
    cout << "The index of pair {23, 30} is "
         << om.order_of_key({ 23, 30 }) << endl;
 
    // Pair {23, 40} is not in the map but it will show the
    // index number if it was there in sorted array
    cout << "The index of pair {23, 40} is "
         << om.order_of_key({ 23, 40 }) << endl;
 
    return 0;
}
输出:
Contents of map:
KEY    ELEMENT
12    22
12    35
23    10
23    20
23    30
The value at 3rd index is {23, 20}
The index of pair {23, 30} is 4
The index of pair {23, 40} is 5

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。