std::getlinestd::getline is a powerful function in C++ that allows you to read input from a stream (such as std::cin) line by line. It's particularly useful when you need to handle input that contains whitespace or when you want to read an entire line of text at once. In this guide, we'll explore various examples of using std::getline in different scenarios.
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter a line of text: ";
std::getline(std::cin, input);
std::cout << "You entered: " << input << std::endl;
return 0;
}
This example demonstrates the most basic usage of std::getline. It reads a line of text from the standard input (std::cin) and stores it in a std::string variable. The function will read until it encounters a newline character or reaches the end of the input.
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> lines;
std::string line;
std::cout << "Enter multiple lines of text (Ctrl+D to finish on Unix, Ctrl+Z on Windows):\n";
while (std::getline(std::cin, line)) {
lines.push_back(line);
}
std::cout << "\nYou entered " << lines.size() << " lines:\n";
for (const auto& l : lines) {
std::cout << l << std::endl;
}
return 0;
}
std::getline in a loop to read multiple lines of input.std::vector<std::string>.#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string input = "apple,banana,cherry,date";
std::istringstream iss(input);
std::string fruit;
std::cout << "Fruits:\n";
while (std::getline(iss, fruit, ',')) {
std::cout << "- " << fruit << std::endl;
}
return 0;
}
std::getline with a custom delimiter.std::istringstream to read from a string instead of std::cin.std::getline specifies ',' as the delimiter.#include <iostream>
#include <string>
int main() {
std::string input;
int lineCount = 0;
std::cout << "Enter text (empty line to finish):\n";
while (true) {
std::getline(std::cin, input);
if (input.empty()) {
break;
}
++lineCount;
std::cout << "Line " << lineCount << ": " << input << std::endl;
}
std::cout << "Total non-empty lines: " << lineCount << std::endl;
return 0;
}
std::getline.empty() method of std::string.Error Handling: std::getline sets the stream's error state if it encounters an error (e.g., end of file). You can check this using std::cin.fail() or std::cin.eof().
Mixing with Other Input Methods: Be cautious when mixing std::getline with other input methods like std::cin >>. The newline character left in the input buffer can cause unexpected behavior.
Performance: For large inputs, consider using std::getline with a std::stringstream or reading chunks of data into a buffer for better performance.
std::getline is a versatile function for reading input in C++. It's particularly useful for:
The examples provided cover basic usage with std::cin, reading multiple lines, using custom delimiters, and handling empty lines. When using std::getline, remember to consider error handling, potential interactions with other input methods, and performance implications for large inputs. With these considerations in mind, std::getline can greatly simplify many input processing tasks in C++ programs.