<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與整型或浮點型互轉 教科書中很少會提到string與int或是float的相互轉換,但是在實際工程中會經常遇到,尤其在做UI控件顯示的時候。比如說你要在edit控件中顯示一個數值,那你就需要把這個數值首先轉為string,然后再將這個string付給edit控件。 網上你會找到很多的轉換方法,個人覺得效率差不多的情況下,簡潔最好。 這里主要用到的是**stringstreams**: stringstream 是 C++ 提供的另一個字串型的串流(stream)物件,和之前學過的 iostream、fstream 有類似的操作方式。要使用 stringstream, 必須先加入這一行: ~~~ #include <sstream> ~~~ stringstream 主要是用在將一個字串分割,可以先用 clear( )以及 str( ) 將指定字串設定成一開始的內容,再用 >> 把個別的資料輸出,例如: ~~~ string s; stringstream ss; int a, b, c; getline(cin, s); ss.clear(); ss.str(s); ss >> a >> b >> c; ~~~ 下面就言歸正傳。? **1、stringstreams中number to string**? 主要是兩步走:? 把number輸出到stream? 從stream中得到string ~~~ int Number = 123; string Result; ostringstream convert; convert << Number; Result = convert.str(); ~~~ 可以將上述代碼縮略成一句話: ~~~ int Number = 123; string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str(); ~~~ 這里需要說明的是,number不限于int,float一樣可以工作 **2、stringstreams中string to number**? 同樣需要兩步走:? 根據string構造一個stream? 將value 讀到變量中 ~~~ string Text = "456"; int Result; istringstream convert(Text); if ( !(convert >> Result) ) { Result = 0; //if that fails set 'Result' to 0 } ~~~ 同樣,也可以對上面的代碼進行簡化: ~~~ string Text = "456"; int Number; if ( ! (istringstream(Text) >> Number) ) Number = 0; ~~~ **3、C++11中number string互轉**? C++11為我們提供了更為便利的方法: 整型、浮點型轉string? **std::to_string**? 重載如下:? string to_string (int val);? string to_string (long val);? string to_string (long long val);? string to_string (unsigned val);? string to_string (unsigned long val);? string to_string (unsigned long long val);? string to_string (float val);? string to_string (double val);? string to_string (long double val); **字符串轉整型:**? **stoi, stol, stoll** **字符串轉浮點型:**? **stof, stod, stold** ~~~ int number = 123; string text = to_string(number); text = "456" number = stoi(number); ~~~ **4、C - stdio中的string與number轉換**? Number to String ~~~ int Number = 123; char Result[16]; sprintf ( Result, "%d", Number ); ~~~ String to Number ~~~ char Text[] = "456"; int Result; sscanf ( Text, "%d", &Result ); ~~~ **5、C - stdlib中的string與number轉換**? itoa? atoi? atol? atof? strtol? strtoul? strtod? 但需要注意的是,上面的幾個函數并非標準,盡量少用。
                  <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>

                              哎呀哎呀视频在线观看