c++ - 表示具有 "multiple-combined"键的查找表的数据结构

标签 c++ oop

您将如何设计用于查找的表数据结构?

我基本上要代表下表

Country             Activity        Legal_Age
European            Drink           18
European            Drive           21
American            Drink           21
American            Drive           18

这里我的 Key 是(Country & Activity),值是 Legal_age。

我想到使用 std::map 来分解这个问题(分解成单独的映射),如下所示。

national_Activity_age_map

European            European_Activity_age_map
American            American_Activity_age_map

European_Activity_age_map

Drink           18
Drive           21

American_Activity_age_map

Drink           21
Drive           18

但这里的问题是,随着原始表的列数不断增加,要添加和维护的 map 数量也在不断增加。

假设美国选择为美国公民和非公民规定不同的饮酒年龄。然后我将不得不添加新的映射并修改现有的映射。

是否有一种简单明了的方法来在 DataStructure 中表示此数据,该 DataStructure 接受一个多因子分解键并产生一个值?

下面有一些类似的问题,但没有回答我的具体问题。 What datastructure would you use to represent this format of data?

更新: 我不能在工作中使用 Boost 功能,因为它必须被移植(或类似的东西)。是否有我可以使用的 C++ (gcc 4.1.2) 功能。

最佳答案

尝试 Boost Multi-Index .

The Boost Multi-index Containers Library provides a class template named multi_index_container which enables the construction of containers maintaining one or more indices with different sorting and access semantics. Indices provide interfaces similar to those of STL containers, making using them familiar. The concept of multi-indexing over the same collection of elements is borrowed from relational database terminology and allows for the specification of complex data structures in the spirit of multiply indexed relational tables where simple sets and maps are not enough.

关于c++ - 表示具有 "multiple-combined"键的查找表的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249803/

相关文章:

c++ - NaN 或 false 作为 double 返回值

c++ - 在循环中创建一个对象

Python:递归isinstance检查

perl - 指示一个类是否在 Perl 中实现了一个接口(interface)有多重要?

TypeScript 在静态实现中推断出更通用的类型

javascript - 将函数传递给属性

c++ - 使用 QPixMap 构建带有图像的 QGridLayout?

c++ - 在不创建参数对象的情况下解析 constexpr 函数

c++ - 如果我写一个字符串,fprintf 会将 '\0' 写入文件吗?

java - 如何判断一个对象是否可序列化?