<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之旅 廣告
                ##沒有躲過的坑--std::string初始化、最快速判斷字符串為空 之前說過,記得給變量初始化。 今天突然想到了一個問題,如果聲明了一std::string類型,怎么初始化呢? ~~~ std::string test_string; std::string test_string_empty = ""; std::string test_string_null = NULL;//運行錯誤,而非編譯錯誤 ~~~ 簡單測試: ~~~ #include<iostream> int main() { std::string test_string; std::string test_string_empty = ""; // std::string test_string_null = NULL; if (test_string == "") { std::cout << "test_string is empty!" << std::endl; } if (test_string_empty == "") { std::cout << "test_string_empty is empty!" << std::endl; } return 0; } /*--------------------------------- 輸出: test_string is empty! test_string_empty is empty! -----------------------------------*/ ~~~ 由此可見,聲明一個std::string對象,默認變為空。 **更重要的是 std::string不能與null進行比較!!** 那么判斷一個std::string 為空 是使用empty還是“”呢? ~~~ #include<iostream> int main() { std::string test_string; std::string test_string_empty = ""; // std::string test_string_null = NULL; if (test_string == "") { std::cout << "test_string is empty!" << std::endl; } if (test_string_empty.empty()) { std::cout << "test_string_empty is empty!" << std::endl; } if (test_string_empty.length() == 0) { std::cout << "test_string_empty is empty!" << std::endl; } return 0; } //輸出: //test_string is empty! //test_string_empty is empty! //test_string_empty is empty! ~~~ 三種方法都可以,但是誰更優越呢? 之前搞過一段C Sharp, 猶記得比較長度最快,不知道C++中是不是也是通過長度來判斷字符串是否為空的方法最為效率呢。 太晚了,明天繼續,歡迎指導: ~~~ #include<iostream> #include <ctime> #include<Windows.h> int main() { std::string test_string; std::string test_string_empty = ""; unsigned int ticks1 = 0; unsigned int ticks2 = 0; unsigned int ticks3 = 0; unsigned int ticks4 = 0; unsigned int ticks5 = 0; unsigned int ticks6 = 0; ticks1 = GetTickCount(); if (test_string_empty == "") { for (int i = 0; i < 100000; i++) { } ticks2 = GetTickCount(); } ticks3 = GetTickCount(); if (test_string_empty.empty()) { for (int i = 0; i < 100000; i++) { } ticks4 = GetTickCount(); } ticks5 = GetTickCount(); if (test_string_empty.length() == 0) { for (int i = 0; i < 100000; i++) { } ticks6 = GetTickCount(); } std::cout << ticks2 - ticks1 << std::endl; std::cout << ticks4 - ticks3 << std::endl; std::cout << ticks6 - ticks5 << std::endl; return 0; } //test_string is empty! //test_string_empty is empty! ~~~
                  <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>

                              哎呀哎呀视频在线观看