Read a string line by line using c++

前端 未结 5 1614
不思量自难忘°
不思量自难忘° 2020-12-15 03:30

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.

Ex: I have a string

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 04:03

    #include 
    #include 
    
    int main() {
        std::istringstream f("line1\nline2\nline3");
        std::string line;    
        while (std::getline(f, line)) {
            std::cout << line << std::endl;
        }
    }
    

提交回复
热议问题