如何在C++中使用std::getline()函数?
在本文中,我们将探讨在C++中使用std::getline()函数。如果您想从输入流中读取字符,这是一个非常方便的函数。
让我们找出如何正确地使用它,通过一些例子来进行说明。
C++中std::getline()的基本语法
这个函数从输入流读取字符并将它们放到一个字符串中。
我们需要引入头文件,因为getline()是该文件的一部分。 , getline() shì de .)
尽管这个模板接受参数,我们将只关注字符串输入(字符),因为输出是写入到字符串中。
istream& getline(istream& input_stream, string& output, char delim);
这句话的意思是,getline()函数接受一个输入流,并将其写入输出。可以选择性地使用delim参数来指定分隔符。
这也会返回一个指向相同输入流的引用,但对于大多数情况,我们并不需要这个句柄。
使用std::getline()从输入流中读取
现在我们已经了解了基本的语法,让我们从标准输入流 `std::cin` 中获取字符串的输入。
#include <iostream>
#include <string>
int main() {
// Define a name (String)
std::string name;
std::cout << "Enter the name: ";
// Get the input from std::cin and store into name
std::getline(std::cin, name);
std::cout << "Hello " << name << "!\n";
return 0;
}
输出
Enter the name: JournalDev
Hello JournalDev!
确实,我们能够毫无问题地从std::cin中获取输入!
现在让我们以另一个例子为例,假设我们有一个名为input.txt的文件,其中包含以下内容:
$ cat input.txt
Hello from JournalDev
Second Line of file
Last line
现在让我们一行一行地读取文件,并将它们存储在一个字符串向量中!
核心逻辑将是使用std::getline(file)持续读取,直到输入流达到文件末尾位置。
我们可以轻松地用这个格式来写。
std::ifstream infile("input.txt");
// Temporary buffer
std::string temp;
// Get the input from the input file until EOF
while (std::getline(infile, temp)) {
// Add to the list of output strings
outputs.push_back(temp);
}
以下是完整的代码:
#include <iostream>
#include <string>
#include <vector> // For std::vector
#include <fstream> // For std::ifstream and std::ofstream
int main() {
// Store the contents into a vector of strings
std::vector<std::string> outputs;
std::cout << "Reading from input.txt....\n";
// Create the file object (input)
std::ifstream infile("input.txt");
// Temporary buffer
std::string temp;
// Get the input from the input file until EOF
while (std::getline(infile, temp)) {
// Add to the list of output strings
outputs.push_back(temp);
}
// Use a range-based for loop to iterate through the output vector
for (const auto& i : outputs)
std::cout << i << std::endl;
return 0;
}
输出
Reading from input.txt....
Hello from JournalDev
Second Line of file
Last line
在C++中使用std::getline()函数以分隔符拆分输入。
我们还可以使用分隔参数(delim)使得getline函数按照指定的分隔符字符进行输入分割。
默认情况下,分隔符是\n(换行符)。我们可以更改这个设置,使得getline()函数能够根据其他字符进行输入分割。
让我们将上述示例中的分隔符设置为空格字符’ ‘,然后看看会发生什么!
#include <iostream>
#include <string>
#include <vector> // For std::vector
#include <fstream> // For std::ifstream and std::ofstream
int main() {
// Store the contents into a vector of strings
std::vector<std::string> outputs;
std::cout << "Reading from input.txt....\n";
// Create the file object (input)
std::ifstream infile("input.txt");
// Temporary buffer
std::string temp;
// Get the input from the input file until EOF
while (std::getline(infile, temp, ' ')) {
// Add to the list of output strings
outputs.push_back(temp);
}
// Use a range-based for loop to iterate through the output vector
for (const auto& i : outputs)
std::cout << i << std::endl;
return 0;
}
输出
Reading from input.txt....
Hello
from
JournalDev
Second
Line
of
file
Last
line
的确,我们现在有了我们的空格分隔的字符串!
使用std::getline()可能存在的问题
尽管std::getline()是一个非常有用的函数,但是当与一些输入流(如std::cin)一起使用时,可能会遇到一些问题。
- std::getline() does not ignore any leading white-space / newline characters.
因此,如果在调用getline()之前调用std::cin >> var;,在读取输入变量后,输入流中仍会有一个换行符剩余。
如果你在cin之后立即调用getline(),你会得到一个换行符,因为它是输入流中的第一个字符!
为了避免这种情况,只需添加一个虚拟的std::getline()来消耗这个换行符!
以下程序展示了在使用getline()之前使用cin存在的问题。
#include <iostream>
#include <string>
int main() {
// Define a name (String)
std::string name;
int id;
std::cout << "Enter the id: ";
std::cin >> id;
std::cout << "Enter the Name: ";
// Notice std::cin was the last input call!
std::getline(std::cin, name);
std::cout << "Id: " << id << std::endl;
std::cout << "Name: " << name << "\n";
return 0;
}
产出
Enter the id: 10
Enter the Name: Id: 10
Name:
注意到我根本无法输入名称!由于输入流中有一个尾随换行符,它只是读取了该字符,并且由于它是一个分隔符,所以停止了继续读取!
现在让我们在实际的std::getline()之前加一个虚拟的std::getline()调用。
#include <iostream>
#include <string>
int main() {
// Define a name (String)
std::string name;
int id;
std::cout << "Enter the id: ";
std::cin >> id;
std::cout << "Enter the Name: ";
// Add a dummy getline() call
std::getline(std::cin, name);
// Notice std::cin was the last input call!
std::getline(std::cin, name);
std::cout << "Id: " << id << std::endl;
std::cout << "Name: " << name << "\n";
return 0;
}
产出
Enter the id: 10
Enter the Name: JournalDev
Id: 10
Name: JournalDev
我们终于修复了我们的错误!希望这会让你在盲目使用std::getline()之前多考虑一下。
很遗憾,C++中没有优雅的方法来获取输入,所以我们必须凑合使用现有的方法!
结论
在本文中,我们学习了如何在C++中使用std :: getline()。我们还看了一些例子,这些例子展示了这个函数的威力和陷阱。
参考资料
- cppreference.com page std::getline()
- StackOverflow Question on using std::getline()