使用C ++逐行读取字符串 [英] Read a string line by line using c++

查看:72
本文介绍了使用C ++逐行读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: string 多行,我需要逐行阅读。
请用一个小例子演示一下。

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.

例如:我有一个字符串 string h;

Ex: I have a string string h;

h将是:

Hello there.
How are you today?
I am fine, thank you.

我需要在其中提取 Hello。今天好吗?很好,谢谢。

I need to extract Hello there., How are you today?, and I am fine, thank you. somehow.

推荐答案

#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;
    }
}

这篇关于使用C ++逐行读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆