c++ - 使用 C++ 逐行读取字符串

标签 c++

我有一个 std::string多行,我需要逐行阅读。 请用一个小例子告诉我如何做到这一点。

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

h 将是:

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

我需要提取 Hello there. , How are you today? , 和 I am fine, thank you.不知何故。

最佳答案

#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++ - 使用 C++ 逐行读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5059049/

相关文章:

c++ - vc++ - Ftp 客户端代码 - 上传文件很慢

c++ - 如何更改集合中的对象子成员

c++ - 如何创建对象指针的复制构造函数(深复制)?

c++ - 模板中的模板——从模板类型访问包含的类型

c++ - OpenCV 阻塞了我的 GTK 接口(interface)

c++ - 不需要时静态初始化

c++ - 使用异常中止一系列用户输入 - 好吗?坏的?

c++ - 从消息队列反序列化多个结构时如何避免 "boost::archive::archive_exception"?

c++ - 在数组C++中读取数组内部的项目

C++ vector,这段代码是什么意思?