##實戰c++中的string系列--將string用于switch語句(c++做C#的事兒, switch中break還是return厲害)
作為一個C++程序員,或是出于習慣,或是出于無奈,你多少次這么寫:
~~~
if (!strcmp(pszValue, "Value X"))
DoThis();
else if (!strcmp(pszValue, "Value Y"))
DoThat();
else if (!strcmp(pszValue, "Value Z"))
DoSomethingElse();
else
DontKnowWhatToDo();
~~~
你千百次的問,如果這個時候可以使用switch多好呢?
~~~
switch(strValue)
{
case "Value X":
DoThis();
break;
case "Value Y":
DoThat();
break;
case "Value Z";
DoSomethingElse();
break;
default:
DontKnowWhatToDo();
break;
~~~
上面這段代碼在C Sharp中是合法的,但是作為一個C++程序員,你只能無奈和無奈。
下面就是用enum和std::map完成這個愿望!
~~~
#include <map>
#include <string>
#include <iostream.h>
// Value-Defintions of the different String values
static enum StringValue { evNotDefined,
evStringValue1,
evStringValue2,
evStringValue3,
evEnd };
// Map to associate the strings with the enum values
static std::map<std::string, StringValue> s_mapStringValues;
// User input
static char szInput[_MAX_PATH];
// Intialization
static void Initialize();
int main(int argc, char* argv[])
{
// Init the string map
Initialize();
// Loop until the user stops the program
while(1)
{
// Get the user's input
cout << "Please enter a string (end to terminate): ";
cout.flush();
cin.getline(szInput, _MAX_PATH);
// Switch on the value
switch(s_mapStringValues[szInput])
{
case evStringValue1:
cout << "Detected the first valid string." << endl;
break;
case evStringValue2:
cout << "Detected the second valid string." << endl;
break;
case evStringValue3:
cout << "Detected the third valid string." << endl;
break;
case evEnd:
cout << "Detected program end command. "
<< "Programm will be stopped." << endl;
return(0);
default:
cout << "'" << szInput
<< "' is an invalid string. s_mapStringValues now contains "
<< s_mapStringValues.size()
<< " entries." << endl;
break;
}
}
return 0;
}
void Initialize()
{
s_mapStringValues["First Value"] = evStringValue1;
s_mapStringValues["Second Value"] = evStringValue2;
s_mapStringValues["Third Value"] = evStringValue3;
s_mapStringValues["end"] = evEnd;
cout << "s_mapStringValues contains "
<< s_mapStringValues.size()
<< " entries." << endl;
}
~~~
這里有個特別重要的技巧,那就是為什么把enumeration的第一個設為evNotDefined ?
首先我們要明確std::map::operator[] 的作用:?
1 設置一個key的value?
2 取值。這個時候需要注意,若不存在,才會被插入。
即程序中對于s_mapStringValues,如果szInput 是新的,將會被插入。?
并且 the value默認為0 。?
如果enumeration第一項為evStringValue1,任何一個未知的string value 都會導致一個有效的switch case。所以我們才這么干。
==============================================================?
這里還有個小問題 討論一下,就是switch語句中return厲害還是break厲害:?
代碼:
~~~
switch(s_mapStringValues[szInput])
{
case evStringValue1:
cout << "Detected the first valid string." << endl;
return 0;//還會執行break嗎?
break;
case evStringValue2:
cout << "Detected the second valid string." << endl;
break;
case evStringValue3:
cout << "Detected the third valid string." << endl;
break;
case evEnd:
cout << "Detected program end command. "
<< "Programm will be stopped." << endl;
return(0);
default:
cout << "'" << szInput
<< "' is an invalid string. s_mapStringValues now contains "
<< s_mapStringValues.size()
<< " entries." << endl;
break;
}
~~~
測試,表面,return了 就不會break了。
- 前言
- string與整型或浮點型互轉
- 指定浮點數有效數字并轉為string
- string的替換、查找(一些與路徑相關的操作)
- string的分割、替換(類似string.split或是explode())
- string的初始化、刪除、轉大小寫(construct erase upper-lower)
- string的遍歷(使用下標還是iterator)
- std::string與MFC中CString的轉換
- string到LPCWSTR的轉換
- std:vector<char> 和std:string相互轉換(vector to stringstream)
- CDuiString和string的轉換(duilib中的cduistring)
- string的連接(+= or append or push_back)
- 函數返回局部變量string(引用局部string,局部string的.c_str()函數)
- 將string用于switch語句(c++做C#的事兒, switch中break還是return厲害)
- 不要使用memset初始化string(一定別這么干)