📜  C++中的getline(字符串)

📅  最后修改于: 2021-05-30 10:50:13             🧑  作者: Mango

C++ getline()是一个标准的库函数,用于从输入流中读取字符串或行。它是< 字符串>标头的一部分。 getline()函数从输入流中提取字符,并将其附加到字符串对象,直到遇到定界字符。这样做时,先前在字符串对象str中存储的值将被输入字符串替换(如果有)。
getline()函数可以通过两种方式表示:

句法:

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

2.参数:

  • 是:它是istream类的对象,它告诉函数有关从何处读取输入流的流。
  • str:这是一个字符串对象,从流中读取输入后,将输入存储在此对象中。
  • delim:是分隔字符,它告诉函数在达到该字符后停止读取进一步的输入。

示例:演示在getline()函数使用定界符。

C++
#include  
#include  
 
using namespace std;
 
//macro definitions
#define MAX_NAME_LEN 60  // Maximum len of your name can't be more than 60
#define MAX_ADDRESS_LEN 120  // Maximum len of your address can't be more than 120
#define MAX_ABOUT_LEN 250 // Maximum len of your profession can't be more than 250
 
int main () {
  char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];
 
  cout << "Enter your name: ";
  cin.getline (y_name, MAX_NAME_LEN);
 
  cout << "Enter your City: ";
  cin.getline (y_address, MAX_ADDRESS_LEN);
 
  cout << "Enter your profession (press $ to complete): ";
  cin.getline (about_y, MAX_ABOUT_LEN, '$');    //$ is a delimiter
 
  cout << "\nEntered details are:\n"<<'\n';
  cout << "Name: " << y_name << endl;
  cout << "Address: " << y_address << endl;
  cout << "Profession is: " << about_y << endl;
}


C++
#include  
#include  
 
using namespace std;
 
//macro definitions
#define MAX_NAME_LEN 6  // Maximum length of your name can't be more than 60
#define MAX_ADDRESS_LEN 120  // Maximum length of your address can't be more than 120
#define MAX_ABOUT_LEN 250 // Maximum length of your profession can't be more than 250
 
int main () {
  char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];
 
  cout << "Enter your name: ";
  cin.getline (y_name, MAX_NAME_LEN);
 
  cout << "Enter your City: ";
  cin.getline (y_address, MAX_ADDRESS_LEN);
 
  cout << "Enter your profession (press $ to complete): ";
  cin.getline (about_y, MAX_ABOUT_LEN, '$');    //$ is a delimiter
 
  cout << "\n\nEntered details are:\n\n";
  cout << "Name: " << y_name << endl;
  cout << "Address: " << y_address << endl;
  cout << "Profession is: " << about_y << endl;
}


CPP
// C++ program to demonstrate getline() function
 
#include 
#include 
using namespace std;
 
int main()
{
    string str;
 
    cout << "Please enter your name: \n";
    getline(cin, str);
    cout << "Hello, " << str
         << " welcome to GfG !\n";
 
    return 0;
}


CPP
// C++ program to understand the use of getline() function
 
#include 
using namespace std;
 
int main()
{
    string S, T;
 
    getline(cin, S);
 
    stringstream X(S);
 
    while (getline(X, T, ' ')) {
        cout << T << endl;
    }
 
    return 0;
}


CPP
// C++ program to demonstrate
// anomaly of delimitation of
// getline() function
#include 
#include 
using namespace std;
 
int main()
{
    string name;
    int id;
 
    // Taking id as input
    cout << "Please enter your id: \n";
    cin >> id;
 
    // Takes the empty character as input
    cout << "Please enter your name: \n";
    getline(cin, name);
 
    // Prints id
    cout << "Your id : " << id << "\n";
 
    // Prints nothing in name field
    // as "\n" is considered a valid string
    cout << "Hello, " << name
         << " welcome to GfG !\n";
 
    // Again Taking string as input
    getline(cin, name);
 
    // This actually prints the name
    cout << "Hello, " << name
         << " welcome to GfG !\n";
 
    return 0;
}


输出:

输出

注意:在上面的示例中,如果#define MAX_NAME_LEN 6,那么在这种情况下,如果您超出定义的限制,则在这种情况下,程序将停止执行并退出,该程序适用于与getline()函数一起使用的每个宏。你会得到 输出如下:

C++

#include  
#include  
 
using namespace std;
 
//macro definitions
#define MAX_NAME_LEN 6  // Maximum length of your name can't be more than 60
#define MAX_ADDRESS_LEN 120  // Maximum length of your address can't be more than 120
#define MAX_ABOUT_LEN 250 // Maximum length of your profession can't be more than 250
 
int main () {
  char y_name[MAX_NAME_LEN], y_address[MAX_ADDRESS_LEN], about_y[MAX_ABOUT_LEN];
 
  cout << "Enter your name: ";
  cin.getline (y_name, MAX_NAME_LEN);
 
  cout << "Enter your City: ";
  cin.getline (y_address, MAX_ADDRESS_LEN);
 
  cout << "Enter your profession (press $ to complete): ";
  cin.getline (about_y, MAX_ABOUT_LEN, '$');    //$ is a delimiter
 
  cout << "\n\nEntered details are:\n\n";
  cout << "Name: " << y_name << endl;
  cout << "Address: " << y_address << endl;
  cout << "Profession is: " << about_y << endl;
}

输出:

输出_2nd

在这里,可以理解的是,名称字段的长度大于所定义的限制,这就是程序停止执行并退出的原因。

1.语法:

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

2.第二个声明与第一个声明几乎相同。唯一的区别是,后者具有定界字符,默认情况下为newline(\ n)字符。
参数:

  • 是:它是istream类的对象,它告诉函数有关从何处读取输入流的流。
  • str:这是一个字符串对象,从流中读取输入后,将输入存储在此对象中。

下面的程序演示了getline()函数
范例1:

CPP

// C++ program to demonstrate getline() function
 
#include 
#include 
using namespace std;
 
int main()
{
    string str;
 
    cout << "Please enter your name: \n";
    getline(cin, str);
    cout << "Hello, " << str
         << " welcome to GfG !\n";
 
    return 0;
}

输入:

Harsh Agarwal

输出:

Hello, Harsh Agarwal welcome to GfG!

例2:我们可以使用getline()函数根据一个字符分割一个句子。让我们看一个例子,以了解如何完成它。

CPP

// C++ program to understand the use of getline() function
 
#include 
using namespace std;
 
int main()
{
    string S, T;
 
    getline(cin, S);
 
    stringstream X(S);
 
    while (getline(X, T, ' ')) {
        cout << T << endl;
    }
 
    return 0;
}

输入:

Hello, Faisal Al Mamun. Welcome to GfG!

输出:

Hello,
Faisal
Al
Mamun.
Welcome
to
GfG!

注意:该函数考虑了新的线路或(“\ n”)字符作为划界字符和换行字符为这种函数有效输入。
下面给出了新行如何引起问题的示例:
例子:

CPP

// C++ program to demonstrate
// anomaly of delimitation of
// getline() function
#include 
#include 
using namespace std;
 
int main()
{
    string name;
    int id;
 
    // Taking id as input
    cout << "Please enter your id: \n";
    cin >> id;
 
    // Takes the empty character as input
    cout << "Please enter your name: \n";
    getline(cin, name);
 
    // Prints id
    cout << "Your id : " << id << "\n";
 
    // Prints nothing in name field
    // as "\n" is considered a valid string
    cout << "Hello, " << name
         << " welcome to GfG !\n";
 
    // Again Taking string as input
    getline(cin, name);
 
    // This actually prints the name
    cout << "Hello, " << name
         << " welcome to GfG !\n";
 
    return 0;
}

输入:

7
MOHIT KUMAR

输出:

Your id : 7
Hello,  welcome to GfG !
Hello, MOHIT KUMAR welcome to GfG !

相关文章:

  • 当输入中有空行时,如何在C++中使用getline()?
  • getline()函数和字符数组
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”