如何在速推中为节点分配标签

How to assign labels to nodes in boost?

本文关键字:节点 分配 标签      更新时间:2023-10-16

我知道图的节点数。我想将图的节点分配标签为 A,B,C,D。如果我有 5 个节点,请将其标记为 A,B,C,D,E。如果我有 6 个节点,请将其标记为 A,B,C,D,E,F。您能为此建议任何动态方法吗?

  enum nodes { A, B, C, D, E };
  char name[] = "ABCDE";

你的问题根本不清楚 - 我不明白你为什么需要提升或你想做什么。也就是说,让我们假设:

  • 您有enum节点类型A..Z .

  • 您需要一种方法在运行时将枚举值转换为字符串表示形式。


gcc.godbolt.org 例子。

#include <cstddef>
// Use `enum class` for additional safety.
// Explictly specify the underyling type as we're going to use the
// enum values to access an array.
enum class nodes : std::size_t { A = 0, B, C, D, E, /* ... */ };
// `constexpr` allows this function to work both at run-time and 
// compile-time.
constexpr auto get_char_for(nodes n) noexcept
{
    // Represent the alphabet as a `constexpr` C-style string.
    constexpr const char* letters = "ABCDEFGHIJKLMNOPQRSTUWXYZ"; 
    // Access and return the alphabet letter at position `n`.
    return letters[static_cast<std::size_t>(n)];
}
static_assert(get_char_for(nodes::A) == 'A');