<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 字符串 > 原文: [https://www.programiz.com/cpp-programming/strings](https://www.programiz.com/cpp-programming/strings) #### 在本文中,您將學習如何在 C 中處理字符串。您將學習聲明它們,對其進行初始化以及將它們用于各種輸入/輸出操作。 字符串是字符的集合。 C++ 編程語言中通常使用兩種類型的字符串: * 作為字符串類(標準 C++ 庫字符串類)的對象的字符串 * C 字符串(C 樣式的字符串) * * * ## C 字符串 在 C 編程中,字符集合以數組形式存儲,C++ 編程也支持這種形式。 因此,它稱為 C 字符串。 C 字符串是`char`類型的數組,以空字符(即`\0`)終止(空字符的 ASCII 值為 0)。 * * * ### 如何定義一個 C 字符串? ```cpp char str[] = "C++"; ``` 在上面的代碼中,`str`是一個字符串,包含 4 個字符。 雖然`"C++"`具有 3 個字符,但空字符`\0`會自動添加到字符串的末尾。 * * * ### 定義字符串的替代方法 ```cpp char str[4] = "C++"; char str[] = {'C','+','+','\0'}; char str[4] = {'C','+','+','\0'}; ``` 像數組一樣,不必使用為字符串分配的所有空間。 例如: ```cpp char str[100] = "C++"; ``` * * * ### 示例 1:使用 C++ 字符串讀取單詞 **C++ 程序,用于顯示用戶輸入的字符串。** ```cpp #include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; cin >> str; cout << "You entered: " << str << endl; cout << "\nEnter another string: "; cin >> str; cout << "You entered: "<<str<<endl; return 0; } ``` **輸出** ```cpp Enter a string: C++ You entered: C++ Enter another string: Programming is fun. You entered: Programming ``` 請注意,在第二個示例中,僅顯示`Programming`,而不是`Programming is fun.`。 這是因為提取運算符`>>`在 C 中作為`scanf()`起作用,并且認為空格`""`具有終止字符。 * * * ### 示例 2:用于讀取一行文本的 C++ 字符串 **C++ 程序,用于讀取和顯示用戶輸入的整行。** ```cpp #include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; cin.get(str, 100); cout << "You entered: " << str << endl; return 0; } ``` **輸出** ```cpp Enter a string: Programming is fun. You entered: Programming is fun. ``` 要讀取包含空格的文本,可以使用`cin.get`函數。 該函數有兩個參數。 第一個參數是字符串的名稱(字符串的第一個元素的地址),第二個參數是數組的最大大小。 在上面的程序中,`str`是字符串的名稱,`100`是數組的最大大小。 * * * ## 字符串對象 在 C++ 中,您還可以創建一個用于保存字符串的字符串對象。 與使用`char`數組不同,字符串對象沒有固定的長度,可以根據需要進行擴展。 * * * ### 示例 3:使用字符串數據類型的 C++ 字符串 ```cpp #include <iostream> using namespace std; int main() { // Declaring a string object string str; cout << "Enter a string: "; getline(cin, str); cout << "You entered: " << str << endl; return 0; } ``` **輸出** ```cpp Enter a string: Programming is fun. You entered: Programming is fun. ``` 在該程序中,聲明了字符串`str`。 然后從用戶詢問字符串。 除了使用`cin>>`或`cin.get()`函數外,您還可以使用`getline()`獲得輸入的文本行。 `getline()`函數將輸入流作為第一個參數,即`cin`和`str`作為要存儲的行的位置。 * * * ## 將字符串傳遞給函數 字符串以類似的方式傳遞給函數[數組也傳遞給函數](/cpp-programming/passing-arrays-function "Passing array to a function in C++")。 ```cpp #include <iostream> using namespace std; void display(char *); void display(string); int main() { string str1; char str[100]; cout << "Enter a string: "; getline(cin, str1); cout << "Enter another string: "; cin.get(str, 100, '\n'); display(str1); display(str); return 0; } void display(char s[]) { cout << "Entered char array is: " << s << endl; } void display(string s) { cout << "Entered string is: " << s << endl; } ``` **輸出** ```cpp Enter a string: Programming is fun. Enter another string: Really? Entered string is: Programming is fun. Entered char array is: Really? ``` 在上面的程序中,要求輸入兩個字符串。 它們分別存儲在`str`和`str1`中,其中`str`是`char`數組,而`str1`是`string`對象。 然后,我們有兩個函數`display()`將字符串輸出到字符串上。 這兩個函數之間的唯一區別是參數。 第一個`display()`函數將`char`數組作為參數,而第二個函數將`string`作為參數。 此過程稱為函數重載。 了解有關[函數重載](/cpp-programming/function-overloading "C++ Function overloading")的更多信息。
                  <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>

                              哎呀哎呀视频在线观看