c++ 枚举与字符串 比较
生活随笔
收集整理的這篇文章主要介紹了
c++ 枚举与字符串 比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
讀取字符串,然后將這個字符轉換為對應的枚舉。
如:從屏幕上輸入'a',則轉換為set枚舉中對應的a,源代碼如下:
//關鍵函數為char2enum(str,temp);#include
using namespace std;
enum set {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};
void char2enum(char ch , set &em)
{// 實現字符轉枚舉,其中參數em為set的引用類型,來把ch轉換的結果傳出char start = 'a';em = (set)((int)ch - (int)start);
}
void main()
{ <pre name="code" class="cpp"> char str;set temp;cout >str; // 獲取從屏幕中輸入的字符char2enum(str, temp); // 將字符str轉換為枚舉tempswitch(temp) // 根據枚舉的值,進行輸出 ,,關鍵語句!!!{case a:cout<<'a'<<endl;break;case b:cout<<'b'<<endl;break;case c:cout<<'c'<<endl;break;case d:cout<<'d'<<endl;break;case e:cout<<'e'<<endl;break;case f:cout<<'f'<<endl;break;// case g:...// case h:...}
}
測試結果:
please input a char:
a 回車
a // 為輸出內容
總結
以上是生活随笔為你收集整理的c++ 枚举与字符串 比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab中 注意事项--字符串
- 下一篇: C++的Matlab接口