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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C++ 中的字符串 > 原文: [https://beginnersbook.com/2017/08/strings-in-c/](https://beginnersbook.com/2017/08/strings-in-c/) 字符串是由字符組成的單詞,因此它們被稱為字符序列。在 C++ 中,我們有兩種方法來創建和使用字符串:1)通過創建`char`數組并將它們視為字符串 2)通過創建`string`對象 讓我們先討論這兩種創建字符串的方法,然后我們會看到哪種方法更好,為什么。 ## 1)字符數組 - 也稱為 C 字符串 **例 1:** 一個簡單例子,我們在聲明期間初始化了`char`數組。 ```cpp #include <iostream> using namespace std; int main(){ char book[50] = "A Song of Ice and Fire"; ? cout<<book; return 0; } ``` **輸出:** ```cpp A Song of Ice and Fire ``` **示例 2:將用戶輸入作為字符串** 這可以被視為讀取用戶輸入的低效方法,為什么?因為當我們使用`cin`讀取用戶輸入字符串時,只有字符串的第一個單詞存儲在`char`數組中而其余部分被忽略。`cin`函數將字符串中的空格視為分隔符,并忽略其后的部分。 ```cpp #include <iostream> using namespace std; int main(){ char book[50]; cout<<"Enter your favorite book name:"; //reading user input cin>>book; cout<<"You entered: "<<book; return 0; } ``` **輸出:** ```cpp Enter your favorite book name:The Murder of Roger Ackroyd You entered: The ``` 你可以看到只有`The`被捕獲在`book`中,空格之后的剩下部分被忽略。那怎么處理呢?那么,為此我們可以使用`cin.get`函數,它讀取用戶輸入的完整行。 **示例 3:使用 `cin.get`** 正確捕獲用戶輸入字符串的方法 ```cpp #include <iostream> using namespace std; int main(){ char book[50]; cout<<"Enter your favorite book name:"; //reading user input cin.get(book, 50); cout<<"You entered: "<<book; return 0; } ``` **輸出:** ```cpp Enter your favorite book name:The Murder of Roger Ackroyd You entered: The Murder of Roger Ackroyd ``` ### 這種方法的缺點 1)`char`數組的大小是固定的,這意味著通過它創建的字符串的大小是固定大小的,在運行時期間不能分配更多的內存。例如,假設您已創建一個大小為 10 的字符數組,并且用戶輸入大小為 15 的字符串,則最后五個字符將從字符串中截斷。 另一方面,如果您創建一個更大的數組來容納用戶輸入,那么如果用戶輸入很小并且數組比需要的大得多,則會浪費內存。 2)在這種方法中,你只能使用為數組創建的內置函數,它們對字符串操作沒有多大幫助。 **這些問題的解決方案是什么?** 我們可以使用字符串對象創建字符串。讓我們看看我們如何做到這一點。 ## C++ 中的`string`對象 到目前為止我們已經看到了如何使用`char`數組處理 C++ 中的字符串。讓我們看看在 C++ 中處理字符串的另一種更好的方法 - 字符串對象。 ```cpp #include<iostream> using namespace std; int main(){ // This is how we create string object string str; cout<<"Enter a String:"; /* This is used to get the user input * and store it into str */ getline(cin,str); cout<<"You entered: "; cout<<str<<endl; /* This function adds a character at * the end of the string */ str.push_back('A'); cout<<"The string after push_back: "<<str<<endl; /* This function deletes a character from * the end of the string */ str.pop_back(); cout << "The string after pop_back: "<<str<<endl; return 0; } ``` **輸出:** ```cpp Enter a String:XYZ You entered: XYZ The string after push_back: XYZA The string after pop_back: XYZ ``` 使用這種方法的好處是你不需要聲明字符串的大小,大小是在運行時確定的,所以這是更好的內存管理方法。內存在運行時動態分配,因此不會浪費內存。
                  <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>

                              哎呀哎呀视频在线观看