使用 C++ 逐行读取字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5059049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a string line by line using c++
提问by raagavan
I have a std::string
with multiple lines and I need to read it line by line.
Please show me how to do it with a small example.
我有一个std::string
多行,我需要逐行阅读。请用一个小例子告诉我如何做到这一点。
Ex: I have a string string h;
例如:我有一个字符串 string h;
h will be:
h 将是:
Hello there.
How are you today?
I am fine, thank you.
I need to extract Hello there.
, How are you today?
, and I am fine, thank you.
somehow.
我需要以某种方式提取Hello there.
, How are you today?
, 和I am fine, thank you.
。
回答by Martin Stone
#include <sstream>
#include <iostream>
int main() {
std::istringstream f("line1\nline2\nline3");
std::string line;
while (std::getline(f, line)) {
std::cout << line << std::endl;
}
}
回答by CashCow
There are several ways to do that.
有几种方法可以做到这一点。
You can use std::string::find
in a loop for '\n'
characters and substr() between the positions.
您可以std::string::find
在'\n'
位置之间的字符和 substr()循环中使用。
You can use std::istringstream
and std::getline( istr, line )
(Probably the easiest)
您可以使用std::istringstream
和std::getline( istr, line )
(可能是最简单的)
You can use boost::tokenize
您可以使用 boost::tokenize
回答by DhruvPathak
this would help you : http://www.cplusplus.com/reference/iostream/istream/getline/
这会帮助你:http: //www.cplusplus.com/reference/iostream/istream/getline/
回答by ericcurtin
If you'd rather not use streams:
如果您不想使用流:
int main() {
string out = "line1\nline2\nline3";
size_t start = 0;
size_t end;
while (1) {
string this_line;
if ((end = out.find("\n", start)) == string::npos) {
if (!(this_line = out.substr(start)).empty()) {
printf("%s\n", this_line.c_str());
}
break;
}
this_line = out.substr(start, end - start);
printf("%s\n", this_line.c_str());
start = end + 1;
}
}
回答by abhiarora
I was looking for some standard implementation for a function which can return a particular line from a string. I came across this question and the accepted answer is very useful. I also have my own implementation which I would like to share:
我正在寻找可以从字符串返回特定行的函数的一些标准实现。我遇到了这个问题,接受的答案非常有用。我也有自己的实现,我想分享一下:
// CODE: A
std::string getLine(const std::string& str, int line)
{
size_t pos = 0;
if (line < 0)
return std::string();
while ((line-- > 0) and (pos < str.length()))
pos = str.find("\n", pos) + 1;
if (pos >= str.length())
return std::string();
size_t end = str.find("\n", pos);
return str.substr(pos, (end == std::string::npos ? std::string::npos : (end - pos + 1)));
}
But I have replaced my own implementation with the one shown in the accepted answer as it uses standard function and would be less bug-prone..
但是我已经用接受的答案中显示的实现替换了我自己的实现,因为它使用标准功能并且不太容易出错..
// CODE: B
std::string getLine(const std::string& str, int lineNo)
{
std::string line;
std::istringstream stream(str);
while (lineNo-- >= 0)
std::getline(stream, line);
return line;
}
There is behavioral difference between the two implementations. CODE: B
removes the newline from each line it returns. CODE: A
doesn't remove newline.
两种实现之间存在行为差异。CODE: B
从它返回的每一行中删除换行符。CODE: A
不删除换行符。
My intention of posting my answer to this not-active question is to make others see possible implementations.
我发布我对这个不活跃问题的回答的目的是让其他人看到可能的实现。
NOTE:
笔记:
I didn't want any kind of optimization and wanted to perform a task given to me in a Hackathon!
我不想要任何类型的优化,而是想要执行在 Hackathon 中给我的任务!