Enum in C,C++
2023-12-18 21:40:23
C(C11以上):
第一种方式:
/**
* @file duLangMap.h
* @author geovindu,Geovin Du(geovindu@163.com)
* @brief vscode c11
* @version 0.1
* @date 2023-12-18
*
* @copyright Copyright (c) 站在巨人的肩膀上 Standing on the Shoulders of Giants 2023
*
*/
#ifndef DULANGMAP_H
#define DULANGMAP_H
#define ChinseLangsList \
DUX(afdu) \
DUX(aldu) \
DUX(ardu) \
DUX(azdu) \
DUX(bgdu) \
DUX(cadu) \
DUX(czdu) \
DUX(dadu) \
DUX(dedu) \
DUX(eldu) \
DUX(endu) \
DUX(eudu) \
DUX(fadu) \
DUX(fidu) \
DUX(frdu) \
DUX(gldu) \
DUX(hedu) \
DUX(hidu) \
DUX(hrdu) \
DUX(hudu) \
DUX(iddu) \
DUX(itdu) \
DUX(jadu) \
DUX(krdu) \
DUX(ladu) \
DUX(ltdu) \
DUX(mkdu) \
DUX(nodu) \
DUX(nldu) \
DUX(pldu) \
DUX(ptdu) \
DUX(pt_brdu) \
DUX(rodu) \
DUX(rudu) \
DUX(svdu) \
DUX(sedu) \
DUX(skdu) \
DUX(sldu) \
DUX(spdu) \
DUX(esdu) \
DUX(srdu) \
DUX(thdu) \
DUX(trdu) \
DUX(uadu) \
DUX(ukdu) \
DUX(vidu) \
DUX(zh_cndu) \
DUX(zh_twdu) \
DUX(zudu)
typedef enum
{
#define DUX(name) name,
ChinseLangsList
#undef DUX
Chinse_Count
} numbers_t;
/**
* @brief
*
*/
static const char* LangDuToString[Chinse_Count] =
{
#define DUX(name) [name] = #name,
ChinseLangsList
#undef DUX
};
#endif
第二种试:
/**
* @file LanguageList.h
* @author geovindu,Geovin Du,涂聚文 (geovindu@163.com)
* @brief
* IDE: vscode c11
* @version 0.1
* @date 2023-12-18
*
* @copyright Copyright (c) 站在巨人的肩膀上 Standing on the Shoulders of Giants 2023
*
*/
#ifndef LANGUAGELIST_H
#define LANGUAGELIST_H
#define EnumLangTypeCase(x) case x: return(#x);
/**
* @brief 语种类型
*
*/
enum DuLang
{
af= 0,// Afrikaans
al=1 ,// Albanian
ar=2 ,// Arabic
az=3 ,// Azerbaijani
bg=4 ,// Bulgarian
ca=5 ,// Catalan
cz=6 ,// Czech
da=7 ,// Danish
de=8 ,// German
el=9 ,// Greek
en=10 ,// English
eu=11 ,// Basque
fa=12 ,// Persian (Farsi)
fi=13,// Finnish
fr=14,// French
gl=15 ,// Galician
he=16 ,// Hebrew
hi=17,// Hindi
hr=18 ,// Croatian
hu=19,// Hungarian
id=20,// Indonesian
it=21,// Italian
ja=22 ,// Japanese
kr=23 ,// Korean
la=24,// Latvian
lt=25,// Lithuanian
mk=26 ,// Macedonian
no=27 ,// Norwegian
nl=28 ,// Dutch
pl=29,// Polish
pt=30,// Portuguese
pt_br=31,// Português Brasil
ro=32,// Romanian
ru=33,// Russian
sv=34 ,// se Swedish
se=35,
sk=36 ,// Slovak
sl=37 ,// Slovenian
sp=38 ,// es Spanish
es=39,
sr=40 ,// Serbian
th=41 ,// Thai
tr=42,// Turkish
ua=43,// uk Ukrainian
uk=44,
vi=45,// Vietnamese
zh_cn=46 ,// Chinese Simplified
zh_tw=47 ,// Chinese Traditional
zu=48 // Zulu
};
/**
* @brief
*
*/
static inline const char *LangtoString(enum DuLang type)
{
switch (type){
EnumLangTypeCase(af)// Afrikaans
EnumLangTypeCase(al)// Albanian
EnumLangTypeCase(ar)// Arabic
EnumLangTypeCase(az)// Azerbaijani
EnumLangTypeCase(bg)// Bulgarian
EnumLangTypeCase(ca)// Catalan
EnumLangTypeCase(cz)// Czech
EnumLangTypeCase(da)// Danish
EnumLangTypeCase(de)// German
EnumLangTypeCase(el)// Greek
EnumLangTypeCase(en)// English
EnumLangTypeCase(eu)// Basque
EnumLangTypeCase(fa)// Persian (Farsi)
EnumLangTypeCase(fi)// Finnish
EnumLangTypeCase(fr)// French
EnumLangTypeCase(gl)// Galician
EnumLangTypeCase(he)// Hebrew
EnumLangTypeCase(hi)// Hindi
EnumLangTypeCase(hr)// Croatian
EnumLangTypeCase(hu)// Hungarian
EnumLangTypeCase(id)// Indonesian
EnumLangTypeCase(it)// Italian
EnumLangTypeCase(ja)// Japanese
EnumLangTypeCase(kr)// Korean
EnumLangTypeCase(la)// Latvian
EnumLangTypeCase(lt)// Lithuanian
EnumLangTypeCase(mk)// Macedonian
EnumLangTypeCase(no)// Norwegian
EnumLangTypeCase(nl)// Dutch
EnumLangTypeCase(pl)// Polish
EnumLangTypeCase(pt)// Portuguese
EnumLangTypeCase(pt_br)// Português Brasil
EnumLangTypeCase(ro)// Romanian
EnumLangTypeCase(ru)// Russian
EnumLangTypeCase(sv)// se Swedish
EnumLangTypeCase(se)
EnumLangTypeCase(sk)// Slovak
EnumLangTypeCase(sl)// Slovenian
EnumLangTypeCase(sp)// es Spanish
EnumLangTypeCase(es)
EnumLangTypeCase(sr)// Serbian
EnumLangTypeCase(th)// Thai
EnumLangTypeCase(tr)// Turkish
EnumLangTypeCase(ua)// uk Ukrainian
EnumLangTypeCase(uk) //
EnumLangTypeCase(vi)// Vietnamese
EnumLangTypeCase(zh_cn)// Chinese Simplified
EnumLangTypeCase(zh_tw)// Chinese Traditional
EnumLangTypeCase(zu)// Zulu
}
return "nodata";
}
#endif
调用:
#include "include/duLangMap.h"
#include "include/LanguageList.h"
int main()
{
printf("hello world, c \n");
printf("你好,中国\n");
//简体中文 46
printf("%s\n",LangtoString(46));
printf("%s\n",LangtoString(zh_cn));
printf("%s\n",LangDuToString[2]);
printf("%s\n",LangDuToString[zh_cndu]);
return 0
}
输出:
C++(C++14以上):
第一种方式:
/*****************************************************************//**
* \file LanguageList.h
* \brief
* https://belaycpp.com/2021/08/24/best-ways-to-convert-an-enum-to-a-string/
* \ IDE vs 2022 C++ 20
* \author geovindu
* \date December 18 2023
* \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
*********************************************************************/
#pragma once
#ifndef LANGUAGELIST_H
#define LANGUAGELIST_H
#include <iostream>
/**
* \brief
*
*/
enum class Lang //:string .net 8
{
af,// Afrikaans
al,// Albanian
ar,// Arabic
az,// Azerbaijani
bg,// Bulgarian
ca,// Catalan
cz,// Czech
da,// Danish
de,// German
el,// Greek
/// <summary>
/// 英文
/// </summary>
en,// English
eu,// Basque
fa,// Persian (Farsi)
fi,// Finnish
fr,// French
gl,// Galician
he,// Hebrew
hi,// Hindi
hr,// Croatian
hu,// Hungarian
id,// Indonesian
it,// Italian
ja,// Japanese
kr,// Korean
la,// Latvian
lt,// Lithuanian
mk,// Macedonian
no,// Norwegian
nl,// Dutch
pl,// Polish
pt,// Portuguese
pt_br,// Português Brasil
ro,// Romanian
ru,// Russian
sv,// se Swedish
se,
sk,// Slovak
sl,// Slovenian
sp,// es Spanish
es,
sr,// Serbian
th,// Thai
tr,// Turkish
ua,// uk Ukrainian
uk,
vi,// Vietnamese
/// <summary>
/// 简体中文
/// </summary>
zh_cn,// Chinese Simplified
/// <summary>
/// 繁体中文
/// </summary>
zh_tw,// Chinese Traditional
zu// Zulu
};
/**
* \brief
*
* \param l
* \return
*/
const char* LangToString(Lang l) noexcept
{
switch (l)
{
case Lang::af: return "af";// Afrikaans
case Lang::al: return "al";// Albanian
case Lang::ar: return "ar";// Arabic
case Lang::az: return "az";// Azerbaijani
case Lang::bg: return "bg";// Bulgarian
case Lang::ca: return "ca";// Catalan
case Lang::cz: return "cz";// Czech
case Lang::da: return "da";// Danish
case Lang::de: return "de";// German
case Lang::el: return "el";// Greek
case Lang::en: return "en";// English
case Lang::eu: return "eu";// Basque
case Lang::fa: return "fa";// Persian (Farsi)
case Lang::fi: return "fi";// Finnish
case Lang::fr: return "fr";// French
case Lang::gl: return "gl";// Galician
case Lang::he: return "he";// Hebrew
case Lang::hi: return "hi";// Hindi
case Lang::hr: return "hr";// Croatian
case Lang::hu: return "hu";// Hungarian
case Lang::id: return "id";// Indonesian
case Lang::it: return "it";// Italian
case Lang::ja: return "ja";// Japanese
case Lang::kr: return "kr";// Korean
case Lang::la: return "la";// Latvian
case Lang::lt: return "lt";// Lithuanian
case Lang::mk: return "mk";// Macedonian
case Lang::no: return "no";// Norwegian
case Lang::nl: return "nl";// Dutch
case Lang::pl: return "pl";// Polish
case Lang::pt: return "pt";// Portuguese
case Lang::pt_br: return "pt_br";// Português Brasil
case Lang::ro: return "ro";// Romanian
case Lang::ru: return "ru";// Russian
case Lang::sv: return "sv";// se Swedish
case Lang::se: return "se";
case Lang::sk: return "sk";// Slovak
case Lang::sl: return "sl";// Slovenian
case Lang::sp: return "sp";// es Spanish
case Lang::es: return "es";
case Lang::sr: return "sr";// Serbian
case Lang::th: return "th";// Thai
case Lang::tr: return "tr";// Turkish
case Lang::ua: return "ua";// uk Ukrainian
case Lang::uk: return "uk";
case Lang::vi: return "vi";// Vietnamese
case Lang::zh_cn: return "zh_cn";// Chinese Simplified
case Lang::zh_tw: return "zh_tw";// Chinese Traditional
case Lang::zu: return "zu";// Zulu
default: throw std::invalid_argument("nodata");
}
}
#endif
调用:
#include "LanguageList.h"
using namespace std;
int main()
{
std::cout << "Hello World!涂聚文 Geovin Du\n";
std::cout<< LangToString(Lang::zh_cn)<< std::endl;
return 0;
}
第二种方式:
/*****************************************************************//**
* \file DuLanguage.h
* \brief 语言种类
*
* \ IDE vs 2022 C++ 20
* \author geovindu
* \date December 18 2023
* \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
*********************************************************************/
#pragma once
#ifndef DULANGUAGE_H
#define DULANGUAGE_H
#include <iostream>
/**
* @brief .
*/
#define DuLang(name, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30,v31,v32,v33,v34,v35,v36,v37,v38,v39,v40,v41,v42,v43,v44,v45,v46,v47,v48,v49)\
enum name { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30,v31,v32,v33,v34,v35,v36,v37,v38,v39,v40,v41,v42,v43,v44,v45,v46,v47,v48,v49 };\
const char *name##Strings[] = { #v1, #v2, #v3, #v4, #v5, #v6, #v7, #v8, #v9, #v10,#v11,#v12,#v13,#v14,#v15,#v16,#v17,#v18,#v19,#v20,#v21,#v22,#v23,#v24,#v25,#v26,#v27,#v28,#v29,#v30,#v31,#v32,#v33,#v34,#v35,#v36,#v37,#v38,#v39,#v40,#v41,#v42,#v43,#v44,#v45,#v46,#v47,#v48,#v49 };\
const char *name##ToString(int value) { return name##Strings[value]; }
/**
* @brief 语言类型.
*/
DuLang(dLang, af,// Afrikaans
al,// Albanian
ar,// Arabic
az,// Azerbaijani
bg,// Bulgarian
ca,// Catalan
cz,// Czech
da,// Danish
de,// German
el,// Greek
/**
* \brief 英语.
*/
en,// English
eu,// Basque
fa,// Persian (Farsi)
fi,// Finnish
fr,// French
gl,// Galician
he,// Hebrew
hi,// Hindi
hr,// Croatian
hu,// Hungarian
id,// Indonesian
it,// Italian
ja,// Japanese
kr,// Korean
la,// Latvian
lt,// Lithuanian
mk,// Macedonian
no,// Norwegian
nl,// Dutch
pl,// Polish
pt,// Portuguese
pt_br,// Português Brasil
ro,// Romanian
ru,// Russian
sv,// se Swedish
se,
sk,// Slovak
sl,// Slovenian
sp,// es Spanish
es,
sr,// Serbian
th,// Thai
tr,// Turkish
ua,// uk Ukrainian
uk,
vi,// Vietnamese
/**
* @brief 简体中文.
*/
zh_cn,// Chinese Simplified
/**
* @brief 繁体中文.
*/
zh_tw,// Chinese Traditional
zu// Zulu
);
#endif
调用:
#include "DuLanguage.h"
using namespace std;
int main()
{
std::cout << "Hello World!涂聚文 Geovin Du\n";
//int a = 2;
//std::cout<< LangToString(Lang::zh_cn)<< std::endl;
std::cout << dLangToString(dLang::zh_cn)<< std::endl;
return 0;
}
第三种方式:
/*****************************************************************//**
* \file ChineseLang.h
* \brief 语言类型枚举
* https://stackoverflow.com/questions/201593/is-there-a-simple-way-to-convert-c-enum-to-string
* \ IDE vs 2022 C++ 20
* \author geovindu
* \date December 18 2023
* \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
*********************************************************************/
#pragma once
#ifndef CHINESELANG_H
#define CHINESELANG_H
/**
* @brief,不可以再用.如果有类似的语言类型枚举,这个不可以用,互相冲突
* 格式关键
*/
#define DEF_ChinseLang \
Du(af, "af") \
Du(al, "al") \
Du(ar, "ar") \
Du(az, "az") \
Du(bg, "bg") \
Du(ca, "ca") \
Du(cz, "cz") \
Du(da, "da") \
Du(de, "de") \
Du(el, "el") \
Du(en, "en") \
Du(eu, "eu") \
Du(fa, "fa") \
Du(fi, "fi") \
Du(fr, "fr") \
Du(gl, "gl") \
Du(he, "he") \
Du(hi, "hi") \
Du(hr, "hr") \
Du(hu, "hu") \
Du(id, "id") \
Du(it, "it") \
Du(ja, "ja") \
Du(kr, "kr") \
Du(la, "la") \
Du(lt, "lt") \
Du(mk, "mk") \
Du(no, "no") \
Du(nl, "nl") \
Du(pl, "pl") \
Du(pt, "pt") \
Du(pt_br, "pt_br") \
Du(ro, "ro") \
Du(ru, "ru") \
Du(sv, "sv") \
Du(se, "se") \
Du(sk, "sk") \
Du(sl, "sl") \
Du(sp, "sp") \
Du(es, "es") \
Du(sr, "sr") \
Du(th, "th") \
Du(tr, "tr") \
Du(ua, "ua") \
Du(uk, "uk") \
Du(vi, "vi") \
Du(zh_cn, "zh_cn") \
Du(zh_tw, "zh_tw") \
Du(zu, "zu")
enum ChinseLang {
#define Du(name, str) name,
DEF_ChinseLang
#undef Du
CHINESE_count
};
/**
* @brief.
*
* \param c
* \return
*/
const char* ChinseLangToStr(ChinseLang c) {
switch (c) {
#define Du(name, str) case name : return str;
DEF_ChinseLang
#undef Du
default:
return "";
}
}
#endif
调用:
#include "ChineseLang.h"
int main()
{
std::cout << "Hello C++ World! 涂聚文 Geovin Du\n";
std::cout << ChinseLangToStr(ChinseLang(2)) << std::endl; //0 索引开始
std::cout << ChinseLangToStr(ChinseLang(al)) << std::endl;
return 0;
}
主要参考:
https://en.cppreference.com/w/c/preprocessor/replace
https://codereview.stackexchange.com/questions/237410/macro-to-generate-an-enum-and-an-array-of-strings
https://codereview.stackexchange.com/questions/278448/discussing-approaches-for-converting-enum-to-string
https://kubyshkin.name/posts/c-language-enums-tips-and-tricks/
文章来源:https://blog.csdn.net/geovindu/article/details/135071022
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!