How is getline used?
getline is a function in C++ used to read a line of data from an input stream.
Its basic syntax is as follows:
std::istream& getline (std::istream& is, std::string& str, char delim);
In this case, ‘is’ is the input stream object, ‘str’ is the string object storing the read results, and ‘delim’ is an optional parameter that represents the line ending symbol and defaults to the newline character \n.
The getline function reads characters from the input stream until it encounters a new line character (or reaches the end of the file). The characters read will be stored in the variable “str”, and the new line character will not be stored. If the “delim” parameter is specified, the reading will stop when that character is encountered.
Example of use:
#include <iostream>
#include <string>
int main() {
std::string line;
// 从标准输入读取一行数据
std::getline(std::cin, line);
std::cout << "输入的内容是:" << line << std::endl;
return 0;
}
In the example above, the getline function reads a line of data from standard input and stores it in the line string. The content that was read is then output to the standard output.