C++ 多维数据处理

2022-01-24 00:00:00 oop containers c++ stl

很多时候,我发现自己不得不为多维数据定义一个容器.

Many times, I find myself having to define a container for multi-dimensional data.

举个例子:我有很多芯片,每个芯片有很多寄存器,每个寄存器有很多单元,每个单元有很多晶体管.

Let's take an example: I have many Chips, each Chip has many Registers, each Register has many Cells, and each Cell has many Transistors.

在我的 C++ 程序的某个阶段,我必须读取这些数据,然后我必须使用它.

At some stage of my C++ program I have to read this data, and later I have to use it.

我不能为这些数据使用任何外部存储:文件、数据库等.

I cannot use any external storage for this data: file, data-base, etc.

那么,我应该创建一些多维 STL 容器吗?一张矢量地图,或者类似的东西……?

So, should I create some multi-dimensional STL container? A map of maps of vectors, or something like that... ?

或者我应该为它们中的每一个创建类(结构)?包含晶体管向量的单元类,然后是包含单元向量的寄存器类,等等?但是,如果以后我想通过晶体管而不是芯片访问我的数据怎么办?

Or should I create classes (structs) for each of them? Cell class that contains a vector of Transistors, and then a Register class that contains a vector of Cells, etc? But what if later I want to access my data by Transistor, and not by Chip?

还有其他方法吗?

谢谢

忘了提:我不能使用 boost.

Forgot to mention: I cannot use boost.

推荐答案

为他们实现完整的类.你的代码最终会更干净.

每当我忽略这个公理时,它就会再次困扰我.我根据 std::strings 和 std:pairs 的 std::pairs 实现了一个分层的 3 层字符串集合.它既快速又简单,当我不得不用一个包含额外属性的类替换一个层,然后另一个层时,它非常容易做到.但最后,代码是一团糟,我不乐意记录它.一次又一次地吸取教训……

Whenever I ignore this axiom, it comes back to haunt me. I implemented a hierarchical 3-tiered string collection in terms of std::pairs of std::strings and std:pairs. It was quick and simple, and when I had to replace one layer and then another with a class to contain extra attributes, it was surprisingly easy to do. But in the end, the code was a mess and I wasn't happy documenting it. Lesson learned again, and again, and again...

相关文章