C++ getline Function: Usage Guide

In C++, the getline function is used to read a line of string from an input stream. The basic syntax is as follows:

#include <iostream>
#include <string>

int main() {
    std::string line;
    
    // 从标准输入流中读取一行字符串
    std::getline(std::cin, line);
    
    // 输出读取的字符串
    std::cout << "You entered: " << line << std::endl;
    
    return 0;
}

In the example above, the getline function reads a line of strings from the standard input stream std::cin and stores it in the variable line. Then, the string that was read is outputted to the standard output stream std::cout.

bannerAds