C++ enum 和 string互转

#include "enum.h"
#include <iostream>
#include <assert.h>

BETTER_ENUM(Word, int, Hello, World)
BETTER_ENUM(Channel, int, Cyan = 1, Magenta, Yellow, Black)
BETTER_ENUM(Channel2, int, Red, Green, Blue)

// We will create a map from this function:

constexpr const char* describe(Channel2 channel)
{
	switch (channel) {
	case Channel2::Red:   return "the red channel";
	case Channel2::Green: return "the green channel";
	case Channel2::Blue:  return "the blue channel";
	}

	return "needed for gcc 5";
}

void selfValue() {

	// Here is the map. The actual type is better_enums::map<Channel2, const char*>.
	constexpr auto descriptions = better_enums::make_map(describe);

	std::cout << descriptions[Channel2::Red] << std::endl;

	std::cout << descriptions.from_enum(Channel2::Red) << std::endl;
	std::cout << descriptions.to_enum("the green channel") << std::endl;

	auto not_a_literal = std::string("the blue channel");
	std::cout << descriptions.to_enum(not_a_literal.c_str()) << std::endl;
}

void test() {
	//test();
	std::cout << (+Word::Hello)._to_string() << ", "
		<< (+Word::World)._to_string() << "!"
		<< std::endl;
	// There are three functions:
	//
	//   1. ._to_string
	//   2. ::_from_string
	//   3. ::_from_string_nothrow
	//   1. ::_from_string_nocase
	//   2. ::_from_string_nocase_nothrow
	Channel     channel = Channel::Cyan;
	std::cout << channel._to_string() << " ";
	channel = Channel::_from_string("Magenta");
	std::cout << channel._to_string() << " ";

	better_enums::optional<Channel> maybe_channel =
		Channel::_from_string_nothrow("Yellow");
	if (!maybe_channel)
		std::cout << "error";
	else
		std::cout << maybe_channel->_to_string() << " ";
	channel = Channel::_from_string_nocase("cYaN");
	channel = Channel::_from_integral(2);
	channel = Channel::_from_integral_unchecked(0);
	std::cout << channel._to_integral() << " ";

	// check
	assert(Channel::_is_valid(3));
	assert(Channel::_is_valid("Magenta"));
	assert(Channel::_is_valid_nocase("cYaN"));

	//  Iteration
	for (size_t index = 0; index < Channel::_size(); ++index) {
		const char* name = Channel::_names()[index];
		Channel     channel = Channel::_values()[index];
		std::cout << channel._to_integral() << " ";
	}

	// 自定义字符串映射
	selfValue();

	std::cout << +Channel2::Red << std::endl;
	//Channel2::_size()  Channel2::_name()
}

//C++20
#include <iostream>
#include <string>
#include <type_traits>

// C++20反射假设
template <typename T>
std::string enumToString(T e) {
    // 假设有一个名为getEnumName的反射函数
    return getEnumName(e);
}

// 示例枚举
enum class MyEnum {
    Value1,
    Value2,
    Value3,
};

int main() {
    MyEnum myValue = MyEnum::Value2;
    std::cout << "Enum as string: " << enumToString(myValue) << std::endl;
    return 0;
}

GitHub - aantron/better-enums: C++ compile-time enum to string, iteration, in a single header file

GitHub - Neargye/magic_enum: Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code


创作不易,小小的支持一下吧!

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在C语言中,将枚举类型(enum换为字符串(string)可以通过以下方法实现。 首先,定义一个枚举类型: ```c typedef enum { VALUE_1, VALUE_2, VALUE_3 } MyEnum; ``` 然后,我们可以创建一个与枚举类型对应的字符串数组: ```c const char* enumStrings[] = { "VALUE_1", "VALUE_2", "VALUE_3" }; ``` 接下来,我们可以编写一个函数,将枚举值换为对应的字符串: ```c const char* enumToString(MyEnum value) { return enumStrings[value]; } ``` 现在,我们可以将枚举类型的值传递给该函数,并获得对应的字符串表示: ```c MyEnum enumValue = VALUE_2; const char* stringValue = enumToString(enumValue); printf("%s\n", stringValue); // 输出 "VALUE_2" ``` 这里的关键点是,我们使用一个字符串数组(enumStrings)将枚举类型的值与字符串进行对应。然后,我们可以通过枚举值作为索引来访问相应的字符串,从而实现枚举类型到字符串的换。 ### 回答2: 在C语言中,Enum(枚举类型)是一种用户定义的数据类型,用于将一组相关的常量值定义为一个命名的集合。当需要将Enum类型换为字符串时,可以使用一些方法。 一种常见的方法是使用switch语句将枚举值换为字符串。例如,假设有一个名为Color的枚举类型,包含红色、绿色和蓝色三个常量值。我们可以定义一个函数或者宏,用于将枚举值换为对应的字符串。代码示例如下: ```c #include <stdio.h> typedef enum { RED, GREEN, BLUE } Color; const char* colorToString(Color c) { switch(c) { case RED: return "红色"; case GREEN: return "绿色"; case BLUE: return "蓝色"; default: return "未知颜色"; } } int main() { Color c = GREEN; const char* str = colorToString(c); printf("枚举值为:%d,对应的字符串为:%s\n", c, str); return 0; } ``` 上述代码中,colorToString函数使用switch语句根据枚举值返回对应的字符串表示。在main函数中,将枚举值GREEN传递给该函数,得到的结果为"绿色"。 除了 switch 语句之外,还可以使用数组、结构体等其他方式实现枚举值到字符串的换。 总之,根据枚举类型的不同,可以选择合适的方法将Enum换为string。以上只是其中一种常用的方法,具体实现可以根据实际需求和枚举类型的特点进行灵活选择。 ### 回答3: 在C语言中,将枚举(enum)类型换为字符串(string)的方法如下: 1. 使用字符串数组:可以在枚举定义的同时创建一个与之对应的字符串数组。例如: ```c enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday}; const char* weekdayNames[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; ``` 然后,通过将枚举值作为索引,就可以获取对应的字符串: ```c enum Weekday today = Monday; const char* todayString = weekdayNames[today]; // "Monday" ``` 2. 使用换函数:可以定义一个函数,该函数接受枚举值作为参数,并返回对应的字符串。例如: ```c enum Month {January, February, March, April, May, June, July, August, September, October, November, December}; char* monthToString(enum Month month) { switch (month) { case January: return "January"; case February: return "February"; case March: return "March"; case April: return "April"; case May: return "May"; case June: return "June"; case July: return "July"; case August: return "August"; case September: return "September"; case October: return "October"; case November: return "November"; case December: return "December"; default: return "Invalid month"; } } ``` 然后,可以通过调用该函数将枚举值换为对应的字符串: ```c enum Month today = January; char* todayString = monthToString(today); // "January" ``` 以上是在C语言中将枚举类型换为字符串的两种方法。通过使用字符串数组或定义换函数,我们可以很方便地实现枚举值到字符串的换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值