【问题标题】:How to split a string into a vector by end of line如何通过行尾将字符串拆分为向量
【发布时间】:2020-12-26 07:32:31
【问题描述】:

您好,我正在尝试将传入的请求逐行拆分为向量。

(示例请求)

GET / HTTP/1.1
Host: 192.168.0.51
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

我想将每一行添加到向量中,我该如何实现?我一直在谷歌搜索,但似乎找不到答案。任何帮助表示赞赏。

【问题讨论】:

    标签: c++ string vector


    【解决方案1】:

    简单的解决方案是使用istringstreamgetline

    #include <string>
    #include <vector>
    #include <sstream>
    
    std::string request = ...;
    
    std::istringstream buffer(request);
    std::vector<std::string> lines;
    std::string line;
    while (getline(buffer, line))
    {
        lines.push_back(line);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 2014-01-16
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      相关资源
      最近更新 更多