我不会将依赖项存储在属性中:
include <iostream>
#include <map>
#include <vector>
struct Attribute;
typedef std::vector<Attribute> Attributes;
struct Attribute {
char name;
operator Attributes () const { return Attributes{ 1, *this }; }
};
inline bool operator < (const Attribute& a, const Attribute& b) {
return a.name < b.name;
}
inline bool operator < (const Attributes& a, const Attributes& b) {
return std::lexicographical_compare(
a.begin(), a.end(),
b.begin(), b.end());
}
inline std::ostream& operator << (std::ostream& stream, const Attributes& attributes) {
for(const auto& a: attributes) {
std::cout << a.name;
}
return stream;
}
typedef std::multimap<Attributes, Attributes> AttributeDependencies;
typedef AttributeDependencies::value_type AttributeDependency;
int main(int argc, const char* argv[]) {
Attribute a {'A'};
Attribute b {'B'};
Attribute c {'C'};
Attribute d {'D'};
AttributeDependencies dpendencies;
dpendencies.insert(AttributeDependency(a, b));
dpendencies.insert(AttributeDependency(Attributes{a, b}, c));
dpendencies.insert(AttributeDependency(a, Attributes{b, c}));
dpendencies.insert(AttributeDependency(Attributes{a, b}, Attributes{c, d}));
for(const auto& d: dpendencies) {
std::cout << '{' << d.first << "} -> {" << d.second << "}\n";
}
return 0;
}
注意: std::map 可能是正确的容器,但 std::multimap 适合示例数据。