<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ##實戰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了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看