C++怎么解决不支持字符串枚举?

首先,有两种方法:使用命名空间和字符串常量与使用 enum class 和辅助函数。

表格直观展示

特性使用命名空间和字符串常量使用 enum class 和辅助函数
类型安全性低 - 编译器无法检查字符串有效性,运行时发现错误高 - 编译期类型检查,非法类型在编译期即可发现
性能低 - 字符串比较时间复杂度为 O(n),逐字符比较有开销高 - 枚举值比较时间复杂度为 O(1),查找效率高,平均查找为 O(1)
可维护性中 - 易于理解和使用,但字符串容易拼写错误高 - 易读易维护,枚举类型和映射关系可以方便管理和扩展
代码简洁性中 - 可能需要反复访问命名空间中的字符串常量高 - 代码简洁且易读,枚举结合辅助函数方便处理
安全性中 - 字符串误输入导致运行时错误高 - 通过类型检查避免运行时崩溃
易用性中 - 需要记住和管理字符串常量,容易犯拼写错误高 - 枚举值可以通过智能提示和自动补全,大幅减少错误

代码展示 

使用命名空间和字符串常量 

优点

  1. 直观和易于理解:字符串表示类型或状态非常直观,容易理解和使用。
  2. 与外部系统交互方便:许多外部系统或配置文件使用字符串表示状态或类型,直接使用字符串便于与这些系统交互。

缺点

  1. 类型不安全:任何字符串都可以传入,编译器无法检查其有效性,拼写错误会导致运行时错误。
  2. 性能开销较大:字符串比较和逐字符检查的时间复杂度为 O(n),性能较低,尤其是在处理长字符串时。
  3. 可维护性差:字符串常量需要在代码中多次引用,拼写错误不易发现,维护困难。
namespace ContainerType {const std::string SimpleView = "SimpleView";const std::string SimpleViewPage = "SimpleViewPage";const std::string ListView = "ListView";const std::string ListViewPage = "ListViewPage";const std::string Dialog = "Dialog";
}void handleContainerType(const std::string& type) {if (type == ContainerType::SimpleView) {std::cout << "Handling SimpleView" << std::endl;} else if (type == ContainerType::SimpleViewPage) {std::cout << "Handling SimpleViewPage" << std::endl;} else if (type == ContainerType::ListView) {std::cout << "Handling ListView" << std::endl;} else if (type == ContainerType::ListViewPage) {std::cout << "Handling ListViewPage" << std::endl;} else if (type == ContainerType::Dialog) {std::cout << "Handling Dialog" << std::endl;} else {std::cout << "Unknown type" << std::endl;}
}

使用 enum class 和辅助函数

优点

  1. 类型安全:编译器进行类型检查,非法类型在编译期即可发现,避免运行时错误。
  2. 性能高效:枚举值比较时间复杂度为 O(1),使用哈希表进行字符串到枚举的映射查找平均为 O(1),高效。
  3. 代码可维护性高:枚举值清晰明了,降低拼写错误风险,方便代码维护。
  4. 易于扩展:可以方便地添加新的枚举值和映射关系。

缺点

  1. 稍微复杂:需要维护枚举和映射关系,初次使用时可能需要更多理解。
enum class ContainerType {SimpleView,SimpleViewPage,ListView,ListViewPage,Dialog,Unknown
};// 辅助函数:字符串 -> ContainerType
ContainerType stringToContainerType(const std::string& typeStr) {static const std::unordered_map<std::string, ContainerType> stringToEnumMap = {{"SimpleView", ContainerType::SimpleView},{"SimpleViewPage", ContainerType::SimpleViewPage},{"ListView", ContainerType::ListView},{"ListViewPage", ContainerType::ListViewPage},{"Dialog", ContainerType::Dialog}};auto it = stringToEnumMap.find(typeStr);if (it != stringToEnumMap.end()) {return it->second;}return ContainerType::Unknown;
}void handleContainerType(ContainerType type) {switch (type) {case ContainerType::SimpleView:std::cout << "Handling SimpleView" << std::endl;break;case ContainerType::SimpleViewPage:std::cout << "Handling SimpleViewPage" << std::endl;break;case ContainerType::ListView:std::cout << "Handling ListView" << std::endl;break;case ContainerType::ListViewPage:std::cout << "Handling ListViewPage" << std::endl;break;case ContainerType::Dialog:std::cout << "Handling Dialog" << std::endl;break;default:std::cout << "Unknown type" << std::endl;break;}
}int main() {ContainerType type = ContainerType::SimpleView;handleContainerType(type);type = stringToContainerType("Dialog");handleContainerType(type);return 0;
}

开源项目magic_enum

magic_enum 是一个 C++17 的头文件库,提供了对枚举的静态反射功能。它允许你在不使用宏或样板代码的情况下,轻松地将枚举类型与字符串进行转换,并实现一系列有关枚举的高效操作。有如下类:

enum class Color { RED = 1, BLUE, GREEN, NONE };
功能描述示例代码
枚举值转字符串将枚举值转换为字符串auto color_name = magic_enum::enum_name(color);
字符串转枚举值将字符串转换为枚举值(不区分大小写、二元谓词比较)auto color = magic_enum::enum_cast<Color>(color_name);
auto color = magic_enum::enum_cast<Color>("green", magic_enum::case_insensitive);
auto color = magic_enum::enum_cast<Color>("green", [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); });
整数转枚举值将整数转换为枚举值auto color = magic_enum::enum_cast<Color>(color_integer);
通过索引访问枚举值根据索引获取枚举值Color color = magic_enum::enum_value<Color>(i);
获取枚举值序列获取所有枚举值的序列constexpr auto colors = magic_enum::enum_values<Color>();
获取枚举元素数量获取枚举的元素个数constexpr std::size_t color_count = magic_enum::enum_count<Color>();
获取枚举值的整数获取枚举值对应的整数auto color_integer = magic_enum::enum_integer(color);
auto color_integer = magic_enum::enum_underlying(color);
获取枚举名称序列获取所有枚举值的名称序列constexpr auto color_names = magic_enum::enum_names<Color>();
获取枚举条目序列获取所有枚举值和名称的序列constexpr auto color_entries = magic_enum::enum_entries<Color>();
检查枚举是否包含某个值检查枚举类型是否包含某个值magic_enum::enum_contains(Color::GREEN);
magic_enum::enum_contains<Color>(2);
magic_enum::enum_contains<Color>("GREEN");
获取枚举值的索引获取枚举值在序列中的索引constexpr auto color_index = magic_enum::enum_index(Color::BLUE);
枚举的位运算支持对枚举类型进行位运算using namespace magic_enum::bitwise_operators;
`Flags flags = Flags::A
标志枚举支持对标志枚举进行特殊处理template <> struct magic_enum::customize::enum_range<Directions> { static constexpr bool is_flags = true; };
`auto name = magic_enum::enum_flags_name(Directions::Up
枚举类型的名称获取枚举类型的名称auto type_name = magic_enum::enum_type_name<decltype(color)>();
ENUM 类型换为在IO流的操作符为枚举添加 ostream 和 istream 的支持using magic_enum::iostream_operators::operator<<;
using magic_enum::iostream_operators::operator>>;
检查是否为无范围枚举判断类型是否为无范围枚举magic_enum::is_unscoped_enum<color>::value;
magic_enum::is_unscoped_enum_v<color>;
检查是否为范围枚举判断类型是否为范围枚举magic_enum::is_scoped_enum<direction>::value;
magic_enum::is_scoped_enum_v<direction>;
Bitwise 操作符为枚举类型提供位操作符using namespace magic_enum::bitwise_operators;
`Flags flags = Flags::A
静态存储枚举变量到字符串更轻量的编译时间和不限于 enum_range 的限制constexpr auto color_name = magic_enum::enum_name<color>();

一个简单的示例:

#include <iostream>
#include <string>
#include <magic_enum.hpp>enum class Color { RED = 1, BLUE, GREEN, NONE };int main() {// 枚举值转字符串Color color1 = Color::RED;std::cout << "Color 1: " << magic_enum::enum_name(color1) << std::endl;// 字符串转枚举值std::string color_name{"GREEN"};auto color2 = magic_enum::enum_cast<Color>(color_name);if (color2.has_value()) {std::cout << "Color 2: " << magic_enum::enum_name(color2.value()) << std::endl;}return 0;
}

遇到的问题

main.cpp:3:10: fatal error: 'magic_enum.hpp' file not found #include <magic_enum.hpp> ^~~~~~~~~~~~~~~~ 1 error generated.

解决方法:

  1. 下载并包含 magic_enum 库。你可以从 magic_enum 的 GitHub 页面上下载最新的版本: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
  2. magic_enum.hpp 头文件放在你的项目的合适目录中,例如 include 目录。
  3. 编译 
g++ -std=c++17 -Iinclude main.cpp -o main

如果你使用 CMake 进行项目构建,可以通过 FetchContentmegic_enum 集成到你的项目中。

   cmake_minimum_required(VERSION 3.11)project(MyProject)include(FetchContent)FetchContent_Declare(magic_enumGIT_REPOSITORY https://github.com/Neargye/magic_enum.gitGIT_TAG v0.7.3)FetchContent_MakeAvailable(magic_enum)add_executable(main src/main.cpp)target_link_libraries(main PRIVATE magic_enum::magic_enum)

模拟实现magic_enum

#include <array>
#include <string>
#include <utility>
#include <string_view>template <typename E, E V>
constexpr auto PrettyName()
{std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2};name.remove_prefix(name.find_last_of(" ") + 1);if (name.front() == '(') name.remove_prefix(name.size());return name;
}template <typename E, E V>
constexpr bool IsValidEnum()
{return !PrettyName<E, V>().empty();
}template <int... Seq>
constexpr auto MakeIntegerSequence(std::integer_sequence<int, Seq...>)
{return std::integer_sequence<int, (Seq)...>();
}constexpr auto NormalIntegerSequence = MakeIntegerSequence(std::make_integer_sequence<int, 32>());template <typename E, int... Seq>
constexpr size_t GetEnumSize(std::integer_sequence<int, Seq...>)
{constexpr std::array<bool, sizeof...(Seq)> valid{IsValidEnum<E, static_cast<E>(Seq)>()...};constexpr std::size_t count = [](decltype((valid)) v) constexpr noexcept->std::size_t{auto cnt = std::size_t{0};for (auto b : v) if (b) ++cnt;return cnt;}(valid);return count;
}template <typename E, int... Seq>
constexpr auto GetAllValidValues(std::integer_sequence<int, Seq...>)
{constexpr std::size_t count = sizeof...(Seq);constexpr std::array<bool, count> valid{IsValidEnum<E, static_cast<E>(Seq)>()...};constexpr std::array<int, count> seq{Seq...};std::array<int, GetEnumSize<E>(NormalIntegerSequence)> values{};for (std::size_t i = 0, v = 0; i < count; ++i) if (valid[i]) values[v++] = seq[i];return values;
}template <typename E, int... Seq>
constexpr auto GetAllValidNames(std::integer_sequence<int, Seq...>)
{constexpr std::array<std::string_view, sizeof...(Seq)> names{PrettyName<E, static_cast<E>(Seq)>()...};std::array<std::string_view, GetEnumSize<E>(NormalIntegerSequence)> validNames{};for (std::size_t i = 0, v = 0; i < names.size(); ++i) if (!names[i].empty()) validNames[v++] = names[i];return validNames;
}template <typename E>
constexpr std::string_view Enum2String(E V)
{constexpr auto names = GetAllValidNames<E>(NormalIntegerSequence);constexpr auto values = GetAllValidValues<E>(NormalIntegerSequence);constexpr auto size = GetEnumSize<E>(NormalIntegerSequence);for (size_t i = 0; i < size; ++i) if (static_cast<int>(V) == values[i]) return names[i];return std::to_string(static_cast<int>(V));
}
#include "myenum.h"
#include <iostream>enum class Color
{RED,GREEN,BLUE,
};int main()
{Color c = Color::BLUE;std::cout << Enum2String(c) << std::endl;return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/41684.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2024 年 6 月区块链游戏研报:Pixels 引发 DAU 波动,行业用户留存率差异显著

作者&#xff1a;Stella L (stellafootprint.network) 数据来源&#xff1a;区块链游戏研究页面 2024 年 6 月&#xff0c;加密货币市场遭遇显著回调&#xff0c;比特币跌幅达 7.3%&#xff0c;以太坊更是下跌了 9.8%。此番波动不可避免地波及区块链游戏领域&#xff0c;导致…

Hudi 写入流程(图)

前言 主要为之前总结的源码文章补充流程图。总结一下整体流程说明 之前以Java Client为例,总结了 Insert 源码的整体流程及部分源码,由于各种原因,没有总结完。长时间不看这方面的源码,容易忘记,之前没有总结流程图,现在回忆起来比较麻烦,不如看流程图方便快捷。所以先补…

2024亚太杯中文赛B题洪水灾害的数据分析与预测原创论文分享

大家好&#xff0c;从昨天肝到现在&#xff0c;终于完成了2024年第十四届 APMCM 亚太地区大学生数学建模竞赛B题洪水灾害的数据分析与预测的完整论文啦。 实在精力有限&#xff0c;具体的讲解大家可以去讲解视频&#xff1a; 2024亚太杯中文赛B题洪水灾害预测原创论文保姆级教…

2024/7/6 英语每日一段

More than half of late-teens are specifically calling for more youth work that offers “fun”, with older teenagers particularly hankering for more jollity, according to a study carried out by the National Youth Agency. One in 10 said they have zero option…

量化机器人:金融市场的智能助手

引言 想象一下&#xff0c;在繁忙的金融市场中&#xff0c;有一位不知疲倦、冷静客观的“超级交易员”&#xff0c;它能够迅速分析海量数据&#xff0c;精准捕捉交易机会&#xff0c;并自动完成买卖操作。这位“超级交易员”不是人类&#xff0c;而是我们今天要聊的主角——量…

Vue 3 中集成 ECharts(附一些案例)

Vue 3 中集成 ECharts 的完全指南 引言 在现代Web开发中&#xff0c;Vue 3以其卓越的性能和灵活的Composition API赢得了广泛的关注。而ECharts&#xff0c;作为开源的一个使用JavaScript实现的强大可视化库&#xff0c;以其丰富的图表类型和高度可定制性成为了数据可视化的首…

计算机的错误计算(二十二)

摘要 计算机的错误计算&#xff08;十九&#xff09;展示了计算机的一个错误计算&#xff1a;本应该为 0的算式的结果不为0. 那么&#xff0c;增加计算精度&#xff0c;能确定是0吗&#xff1f;不一定。 计算机的错误计算&#xff08;十九&#xff09;展示了计算机对 的错误计…

适用于 Windows的 5 个最佳 PDF 转 Word 转换器

PDF 文件是共享文档的首选格式&#xff0c;但是&#xff0c;此类文件存在限制&#xff0c;使其难以修改或编辑。因此&#xff0c;您可能会发现自己正在寻找一种将 PDF 文件转换为 Word 或其他可编辑格式的方法。 有许多不同的 PDF 转换器&#xff0c;每个转换器的功能略有不同…

vue require引入静态文件报错

如果是通过向后端发送请求&#xff0c;动态的获取对应的文件数据流很容易做到文件的显示和加载。现在研究&#xff0c;一些不存放在后端而直接存放在vue前端项目中的静态媒体文件如何加载。 通常情况下&#xff0c;vue项目的图片jpg&#xff0c;png等都可以直接在/ass…

招聘一个1-3年经验的Java工程师:企业视角的技能与素质要求

个人名片 &#x1f393;作者简介&#xff1a;java领域优质创作者 &#x1f310;个人主页&#xff1a;码农阿豪 &#x1f4de;工作室&#xff1a;新空间代码工作室&#xff08;提供各种软件服务&#xff09; &#x1f48c;个人邮箱&#xff1a;[2435024119qq.com] &#x1f4f1…

Git错误分析

错误案例1&#xff1a; 原因&#xff1a;TortoiseGit多次安装导致&#xff0c;会记录首次安装路径&#xff0c;若安装路径改变&#xff0c;需要配置最后安装的路径。

Windows搭建本地对象存储服务MinIO并且使用springboot整合

开发文档&#xff1a; MinIO Windows中文文档 MinIO Object Storage for Windows &#xff08;英文文档&#xff09; 1、准备工作 准备一个空的文件夹&#xff0c;用来存放minio相关的内容&#xff1b; 这里是在D盘创建一个minio的文件夹&#xff1b; 后续所有跟MinIO相关…

matlab 超越椭圆函数图像绘制

matlab 超越椭圆函数图像绘制 超越椭圆函数图像绘制xy交叉项引入斜线负向斜线成分正向斜线成分 x^2 y^2 xy 1 &#xff08;负向&#xff09;绘制结果 x^2 y^2 - xy 1 &#xff08;正向&#xff09;绘制结果 超越椭圆函数图像绘制 xy交叉项引入斜线 相对于标准圆&#xf…

ESP32-Wifi问题解答

目录 前言 环境&#xff1a;arduino 芯片&#xff1a;ESP32 一、先上例程 1.大家打开官方例程 点击:示例->Wifi->WifiClient 2:Wifi配置 打开例程如图: 在1处设置WiFi名称 在2处设置WiFi密码 注意:双引号要留着 3:编译,烧录->问题发生了! 二、问题解决方法 1.…

NSK发布新版在线计算工具

July 01, 2024 NSK Ltd. Corporate Communications Department NSK Ltd. announced today that it has improved the engineering tools available on its website. The new engineering tools — NSK Online Catalog, Technical Calculations, and 2D/3D CAD Data — which …

ctfshow-web入门-文件包含(web87)巧用 php://filter 流绕过死亡函数的三种方法

目录 方法1&#xff1a;php://filter 流的 base64-decode 方法 方法2&#xff1a;通过 rot13 编码实现绕过 方法3&#xff1a;通过 strip_tags 函数去除 XML 标签 除了替换&#xff0c;新增 file_put_contents 函数&#xff0c;将会往 $file 里写入 <?php die(大佬别秀了…

E4.【C语言】练习:while和getchar的理解

#include <stdio.h> int main() {int ch 0;while ((ch getchar()) ! EOF){if (ch < 0 || ch>9)continue;putchar(ch);}return 0; } 理解上述代码 0-->48 9-->57 if行判断是否为数字&#xff0c;打印数字&#xff0c;不打印非数字

机器学习——随机森林

随机森林 1、集成学习方法 通过构造多个模型组合来解决单一的问题。它的原理是生成多个分类器/模型&#xff0c;各自独立的学习和做出预测。这些预测最后会结合成组合预测&#xff0c;因此优于任何一个单分类得到的预测。 2、什么是随机森林&#xff1f; 随机森林是一个包含…

[数据结构] 排序#插入排序希尔排序

标题&#xff1a;[数据结构] 排序#插入排序&希尔排序 水墨不写bug 目录 &#xff08;一&#xff09;插入排序 实现思路&#xff1a; 插入排序实现&#xff1a; &#xff08;二&#xff09;希尔排序 希尔排序的基本思想&#xff1a; 希尔排序的实现&#xff1a; 正…

IT入门知识第八部分《人工智能》(9/10)

1.引言 在当今数字化时代&#xff0c;人工智能&#xff08;AI&#xff09;和机器学习&#xff08;ML&#xff09;已成为推动技术革新的关键力量。它们不仅改变了我们与机器的互动方式&#xff0c;还极大地拓展了解决问题的可能性。本文将深入探讨人工智能和机器学习的基础&…