溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

關(guān)于數(shù)組與字符串輸入的問題-C++版

發(fā)布時間:2020-08-10 20:57:02 來源:網(wǎng)絡(luò) 閱讀:2050 作者:liam2199 欄目:移動開發(fā)

對于字符串問題,原來理解的不夠深刻,現(xiàn)在討論一些關(guān)于字符串輸入的問題

關(guān)于數(shù)組與字符串輸入的問題-C++版

   1.strlen() 返回的是數(shù)組中的字符串的長度,而不是數(shù)組本身的長度。

   2.strlen()只計算可見的字符,而不把空字符計算在內(nèi)。


  那么更有意思的在后面:

char name[16] = "abcdefg";
//輸出結(jié)果是多少?
cout << name << endl;
name[3] = '\0';
//輸出結(jié)果又是多少?
cout << name << endl;

   大家猜猜 ?


# include <iostream>
# include <cstring>
# define SIZE 15
using namespace std;
int main(void)
{
    char name_cin[SIZE];
    char name[SIZE] = "C++owboy"; //initialized array
    cout << "Hello I'm " << name;
    cout << "! What is your name ? ";
    cin >> name_cin;
    cout << "Well " << name_cin << ", your name has ";
    cout << strlen(name_cin) << " letters and is stored " << endl;
    cout << "in an array of " << sizeof(name_cin) << "bytes." << endl;
    cout << "your initial is " << name_cin[0] << "." << endl;
    name[3] = '\0';
    cout << "Here are the first 3 characters of my name : ";
    cout << name << endl;
    return 0;
}


   大家猜猜結(jié)果呢?


關(guān)于數(shù)組與字符串輸入的問題-C++版

   name字符串被截斷了...


   釋義:  

   數(shù)組可以用索引來訪問數(shù)組的各個字符,例如name[0]找到數(shù)組的第一個字符,name[3] = '\0';  設(shè)置為空字符,使得這個字符串在第三個字符后面即結(jié)束,即使數(shù)組中還有其他字符。


關(guān)于數(shù)組與字符串輸入的問題-C++版

   不過cin有個缺陷,就是以空白符為結(jié)束標(biāo)志,如果遇到空格和回車就把這個字符串輸入完了,這樣就需要用能輸入一行字符串的方法來解決,但是先看看這個問題:


# include <iostream>
using namespace std;
int main(void)
{
    const int ArSize = 20;
    char name[ArSize];
    char dessert[ArSize];
    cout << "Enter your name : " << endl;
    cin >> name;  //輸入名字
    cout << "Enter your favorite dessert: " << endl;
    cin >> dessert; //輸入甜點的名字
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << "." << endl;
    return 0;
}

關(guān)于數(shù)組與字符串輸入的問題-C++版

   釋義:


cin使用空白(空格、制表符、換行符)來定字符串的邊界,cin在獲取字符數(shù)組輸入時只讀取第一個單詞,讀取單詞后,cin將該字符串放到數(shù)組中,并自動在結(jié)尾添加空字符'\0'

cin把Meng作為第一個字符串,并放到數(shù)組中,把Liang放到輸入隊列中第二次輸入時,發(fā)現(xiàn)輸入隊列Liang,因為cin讀取Liang,并將它放到dessert數(shù)組中


  這時如果能輸入一行數(shù)據(jù),這個問題不就解決了嗎?

   getline()、get()可以實現(xiàn)...

   

# include <iostream>
using namespace std;
int main(void)
{
    const int ArSize = 20;
    char name[ArSize];
    char dessert[ArSize];
    cout << "Enter you name : " << endl;
    cin.getline(name,ArSize);
    cout << "Enter you favorite dessert : " << endl;
    cin.getline(dessert,ArSize);
    cout << "I have some delicious " << dessert;
    cout << " for you," << name << endl;
    return 0;
}


關(guān)于數(shù)組與字符串輸入的問題-C++版

   釋義:

   

   cin是將一個單詞作為輸入,而有些時候我們需要將一行作為輸入,如 I love C++

   iostream中類提供了一些面向行的類成員函數(shù),如getline()和get(),這兩個都是讀取一行的輸入,直到換行符結(jié)束,區(qū)別是getline()將丟棄換行符

   get()將換行符保留在輸入序列中

   面向行的輸入:getline(char* cha,int num)

   getline()讀取整行,通過換行符來確定結(jié)尾,調(diào)用可以使用 cin.getline(char* cha,int num),成員函數(shù)的方式使用,第一個參數(shù)是用來存儲輸入行的數(shù)組的名稱,第二個參數(shù)是要讀取的字符數(shù),如果這個字符數(shù)的參數(shù)為30,則最多讀入29個字符,余下的用于存儲自動在結(jié)尾處添加的空字符。

   get()存儲字符串的時候,用空字符結(jié)尾。


   如果遇到這種情況咋辦?

   

# include <iostream>
using namespace std;
int main(void)
{
    cout << "What year was your house built? " << endl;
    int year;
    cin >> year;
    //char ch;
    //cin.get(ch); 接收換行符  (cin >> year).get();
    cout << "What is its street address ? " << endl;
    char address[80];
    cin.getline(address, 80);
    cout << "Year built : " << year << endl;
    cout << "Address : " << address << endl;
    cout << "Done ! " << endl;
    return 0;
}

   關(guān)于數(shù)組與字符串輸入的問題-C++版

   地址還沒有輸入,就結(jié)束了...

   去掉上面的注意,加一個字符,接收換行符就可以了...



   注:C++程序常使用指針而不是數(shù)組來處理字符串

   


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI