c++ - 如何添加不同类型的属性图?

标签 c++ oop c++11 dynamic-typing

这是一个 C++(11) 设计问题:

假设我想创建一个图形数据结构,您可以在其中将节点/边映射到任意属性。 (它是一个图形并不真正相关,它可能是具有属性的元素的任何容器,但这是我的示例。)

有了这样的数据结构,我可以为所有节点动态添加一个新属性:

Graph G;

G.addNodeMap("color", "white"); // map name, default value

...然后为一个节点设置它:

node v;
G.setAttr("color", v, "blue");

...并删除属性以节省内存:

G.deleteNodeMap("color");

G.addNodeMap采用 map 的标识符(可能是字符串)和条目的默认值。使用 C++11,键入 T可以从给定的默认参数中方便地推断出“节点映射”。节点映射本身可以是 std::vector<T>因为节点只是一个索引。

问题:我在哪里存储 vector std::vector<std::string> map1 , std::vector<std::double> map2 , std::vector<Foo> map3 ……?

动态类型语言不会出现此问题。如何使用 C++ 实现此行为?

最佳答案

您可能想看一下 Boost.Graph Library 的属性映射(BGL)

The main link between the abstract mathematical nature of graphs and the concrete problems they are used to solve is the properties that are attached to the vertices and edges of a graph, things like distance, capacity, weight, color, etc. There are many ways to attach properties to graph in terms of data-structure implementation, but graph algorithms should not have to deal with the implementation details of the properties. The property map interface defined in Section Property Map Concepts provides a generic method for accessing properties from graphs. This is the interface used in the BGL algorithms to access properties.

关于c++ - 如何添加不同类型的属性图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14707077/

相关文章:

c++ - 合并两个显示亮度的图像

c++ - ifstream --> ofstream C++

ios - swift : create two objects each of them inside another

javascript - 在 JavaScript 中也使用特征的 OOP 的一个很好的例子是什么?

java - 在java中声明一个类

c++ - 如何在 std::set 中存储二维几何 vector

c++ - std::declval<T>() 是如何工作的?

C++ 构造函数代码...这叫什么?

C++ 嵌入式模板模板

c++ - mediafoundation 能否允许多个客户端同时访问单个网络摄像头设备?