Re: Re: [cpp] 怎么高效实现从char[] 到byte的转换?

江旭 jiangxu at boesoft.com
Tue Mar 20 13:29:41 CST 2007


hit_pc,您好!

	我很奇怪,为什么不用STL的map呢?

======= 2007-03-20 13:14:00 您在来信中写道:=======

>
>On Tue, 20 Mar 2007 12:30:06 +0800, Fei Xu <flyingxu at gmail.com> wrote:
>
>> 比如有这样的一个表:
>> mov 01
>> add 02
>> div  03
>> ...
>> end 99
>如果你这个表是编译以前知道的,使用enmu,
>enum week{Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
>
>week today = Tuesday;
>
>switch(today)
>{
>case Monday:
>     cout<<"Monday<<endl";
>     break;
>case Tuesday:
>     cout<<"Tuesday"<<endl;
>     break;
>default:
>     cerr<<"Wrong!!"<<endl;
>     break;
>}
>----------------------------------------------------------------
>如果不是,使用下面这个解决办法:
>// 定义一个用于转义判断的结构
>typedef struct tagShift{
>     // 建议这里用enum,避免出现Magic Number
>     unsigned int uIdx;
>     const char *pStr;
>}Shift,*PShift;
>
>// 声明一张转义表
>Shift g_ShiftTable[] =
>{
>     // 建议这里用enum,避免出现Magic Number
>     { 1, "TestCmd1"},
>     { 2, "TestCmd2"},
>     { 3, "TestCmd3"},
>     { 4, "TestCmd4"},
>     { 5, "TestCmd5"},
>     { 6, "TestCmd6"},
>     { 7, "TestCmd7"},
>     { 0, NULL},
>};
>
>// 实现一个转义函数
>unsigned int GetShiftValue(const char * lpStr)
>{
>     for (PShift *p = g_ShiftTable[0]; p->pStr; p ++)
>     {
>         if (0 == strcmpi(p->pStr, lpStr)) return p->uIdx;
>     }
>     return p->uIdx;
>}
>
>// 在switch语句中这样写
>     const char *pStr = "TestCmd7";
>     switch(GetShiftValue(pStr))
>     {
>         case 0:
>            break;
>         case 1:
>            break;
>         case 2:
>            break;
>         case 3:
>            break;
>         default:
>            break;
>     }
>
>>
>> 现在要根据传入的char*(也就是"move""add"之类)得到对应的数字 01, 02
>>
>> 简单的说当然就是
>> if(c == "")
>>     ...;
>> else if(c == "")
>>     ....;
>>
>> 但是这样写起来很麻烦,执行起来似乎效率也不高,有好办法吗?
>>
>
>
>
>-- 
>Hello,World.
>         -LegolasKiss
>
>
>_______________________________________________
>Cpp mailing list
>Cpp at codingnow.com
>http://codingnow.com/mailman/listinfo/cpp

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        江旭
        jiangxu at boesoft.com
          2007-03-20




More information about the Cpp mailing list