溫馨提示×

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

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

cin.getline與getline()怎么在C++中使用

發(fā)布時(shí)間:2021-03-31 17:11:26 來源:億速云 閱讀:170 作者:Leah 欄目:編程語言

本篇文章為大家展示了cin.getline與getline()怎么在C++中使用,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

使用 C++ 字符數(shù)組與使用 string 對(duì)象還有另一種不同的方式,就是在處理它們時(shí)必須使用不同的函數(shù)集。例如,要讀取一行輸入,必須使用 cin.getline 而不是 getline 函數(shù)。這兩個(gè)的名字看起來很像,但它們是兩個(gè)不同的函數(shù),不可互換。

與 getline 一樣,cin.getline 允許讀取包含空格的字符串。它將繼續(xù)讀取,直到它讀取至最大指定的字符數(shù),或直到按下了回車鍵。以下是其用法示例:

cin.getline(sentence, 20);

getline 函數(shù)使用兩個(gè)用逗號(hào)分隔的參數(shù)。第一個(gè)參數(shù)是要存儲(chǔ)字符串的數(shù)組的名稱。第二個(gè)參數(shù)是數(shù)組的大小。當(dāng) cin.getline 語句執(zhí)行時(shí),cin 讀取的字符數(shù)將比該數(shù)字少一個(gè),為 null 終止符留出空間。這樣就不需要使用 setw 操作符或 width 函數(shù)。以上語句最多可讀取 19 個(gè)字符,null 終止符將自動(dòng)放在數(shù)組最后一個(gè)字符的后面。

下面的程序演示了 getline 函數(shù)的用法,它最多可以讀取 80 個(gè)字符:

// This program demonstrates cinT s getline function
// to read a line of text into a C-string.
#include <iostream>、
using namespace std;
int main()
{
  const int SIZE = 81;
  char sentence[SIZE];
  cout << "Enter a sentence: ";
  cin.getline (sentence, SIZE);
  cout << "You entered " << sentence << endl;
  return 0;
}

程序輸出結(jié)果:

Enter a sentence: To be, or not to be, that is the question.
You entered To be, or not to be, that is the question.

補(bǔ)充:C++ getline()的兩種用法

getline():用于讀入一整行的數(shù)據(jù)。在C++中,有兩種getline函數(shù)。第一種定義在頭文件<istream>中,是istream類的成員函數(shù);第二種定義在頭文件<string>中,是普通函數(shù)。

第一種: 在<istream>中的getline()函數(shù)有兩種重載形式:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

作用是: 從istream中讀取至多n個(gè)字符(包含結(jié)束標(biāo)記符)保存在s對(duì)應(yīng)的數(shù)組中。即使還沒讀夠n個(gè)字符,如果遇到delim標(biāo)識(shí)符或字?jǐn)?shù)達(dá)到限制,則讀取終止。delim標(biāo)識(shí)符會(huì)被讀取,但是不會(huì)被保存進(jìn)s對(duì)應(yīng)的數(shù)組中。注意,delim標(biāo)識(shí)符在指定最大字符數(shù)n的時(shí)候才有效。

#include <iostream>
using namespace std;

int main()
{
 char name[256], wolds[256];
 cout<<"Input your name: ";
 cin.getline(name,256);
 cout<<name<<endl;
 cout<<"Input your wolds: ";
 cin.getline(wolds,256,',');
 cout<<wolds<<endl;
 cin.getline(wolds,256,',');
 cout<<wolds<<endl;
 return 0;
}

輸入

Kevin
Hi,Kevin,morning

輸出

Kevin
Hi
Kevin

第二種: 在<string>中的getline函數(shù)有四種重載形式:

istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);

用法和上第一種類似,但是讀取的istream是作為參數(shù)is傳進(jìn)函數(shù)的。讀取的字符串保存在string類型的str中。

is:表示一個(gè)輸入流,例如cin。

str:string類型的引用,用來存儲(chǔ)輸入流中的流信息。

delim:char類型的變量,所設(shè)置的截?cái)嘧址?;在不自定義設(shè)置的情況下,遇到'\n',則終止輸入。

#include<iostream>
#include<string>
using namespace std;
int main(){
 string str;
 getline(cin, str, 'A');
 cout<<"The string we have gotten is :"<<str<<'.'<<endl;
 getline(cin, str, 'B');
 cout<<"The string we have gotten is :"<<str<<'.'<<endl;
return 0;}

輸入

i_am_A_student_from_Beijing

輸出

The string we have gotten is :i_am_.
The string we have gotten is :_student_from_.

上述內(nèi)容就是cin.getline與getline()怎么在C++中使用,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

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

AI