如何在boost中为节点分配标签?

时间:2016-06-06 12:52:58

标签: c++

我知道图的节点数。我想将图表的节点标记为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";

1 个答案:

答案 0 :(得分:1)

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

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

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

gcc.godbolt.org example

#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');