本文對C++數組與字符串輸入的問題進行了探討,需要的朋友可以參考下
對于字符串問題,原來理解的不夠深刻,現在討論一些關于字符串輸入的問題
1.strlen() 返回的是數組中的字符串的長度,而不是數組本身的長度。
2.strlen()只計算可見的字符,而不把空字符計算在內。
那么更有意思的在后面:
- char name[16] = "abcdefg";
- //輸出結果是多少?
- cout << name << endl;
- name[3] = '/0';
- //輸出結果又是多少?
- 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;
- }
大家猜猜結果呢?
name字符串被截斷了...
釋義:
數組可以用索引來訪問數組的各個字符,例如name[0]找到數組的第一個字符,name[3] = '/0'; 設置為空字符,使得這個字符串在第三個字符后面即結束,即使數組中還有其他字符。
不過cin有個缺陷,就是以空白符為結束標志,如果遇到空格和回車就把這個字符串輸入完了,這樣就需要用能輸入一行字符串的方法來解決,但是先看看這個問題:
- # 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;
- }
釋義:
cin使用空白(空格、制表符、換行符)來定字符串的邊界,cin在獲取字符數組輸入時只讀取第一個單詞,讀取單詞后,cin將該字符串放到數組中,并自動在結尾添加空字符'/0'
cin把Meng作為第一個字符串,并放到數組中,把Liang放到輸入隊列中第二次輸入時,發現輸入隊列Liang,因為cin讀取Liang,并將它放到dessert數組中
這時如果能輸入一行數據,這個問題不就解決了嗎?
getline()、get()可以實現...
- # 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;
- }
釋義:
cin是將一個單詞作為輸入,而有些時候我們需要將一行作為輸入,如 I love C++
iostream中類提供了一些面向行的類成員函數,如getline()和get(),這兩個都是讀取一行的輸入,直到換行符結束,區別是getline()將丟棄換行符
get()將換行符保留在輸入序列中
面向行的輸入:getline(char* cha,int num)
getline()讀取整行,通過換行符來確定結尾,調用可以使用 cin.getline(char* cha,int num),成員函數的方式使用,第一個參數是用來存儲輸入行的數組的名稱,第二個參數是要讀取的字符數,如果這個字符數的參數為30,則最多讀入29個字符,余下的用于存儲自動在結尾處添加的空字符。
get()存儲字符串的時候,用空字符結尾。
如果遇到這種情況咋辦?
- # 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;
- }
地址還沒有輸入,就結束了...
去掉上面的注意,加一個字符,接收換行符就可以了...
注:C++程序常使用指針而不是數組來處理字符串
以上就是本文的全部內容,希望對大家的學習有所幫助。
新聞熱點
疑難解答