【问题标题】:C++ split string by lineC++ 逐行分割字符串
【发布时间】:2012-10-21 18:00:14
【问题描述】:

我需要逐行拆分字符串。 我以前是这样做的:

int doSegment(char *sentence, int segNum)
{
assert(pSegmenter != NULL);
Logger &log = Logger::getLogger();
char delims[] = "\n";
char *line = NULL;
if (sentence != NULL)
{
    line = strtok(sentence, delims);
    while(line != NULL)
    {
        cout << line << endl;
        line = strtok(NULL, delims);
    }
}
else
{
    log.error("....");
}
return 0;
}

我输入“我们是一体的。\是的,我们是。”并调用 doSegment 方法。但是当我调试时,我发现句子参数是“we are one.\\nyes we are”,并且拆分失败。有人可以告诉我为什么会这样,我该怎么办。还有其他我可以用来在 C++ 中拆分字符串的方法吗?谢谢!

【问题讨论】:

  • 我建议使用真正的 c++ - 看看 std::getline
  • 您需要对输入进行一些更改。不是拆分功能。
  • 小心strtok,因为它会修改您传递给它的字符串。

标签: c++ split


【解决方案1】:

您可以循环调用std::string::find 并使用std::string::substr

std::vector<std::string> split_string(const std::string& str,
                                      const std::string& delimiter)
{
    std::vector<std::string> strings;

    std::string::size_type pos = 0;
    std::string::size_type prev = 0;
    while ((pos = str.find(delimiter, prev)) != std::string::npos)
    {
        strings.push_back(str.substr(prev, pos - prev));
        prev = pos + 1;
    }

    // To get the last substring (or only, if delimiter is not found)
    strings.push_back(str.substr(prev));

    return strings;
}

参见示例here

【讨论】:

  • 如果您使用的分隔符包含多个字符,就像我一样,您将需要更改“prev = pos + 1;”行至“prev = pos + delimiter.size();”反而。否则,您将在向量中下一个元素的开头留下剩余的字符。
【解决方案2】:

我想使用 std::getline 或 std::string::find 来遍历字符串。 下面的代码演示了getline函数

int doSegment(char *sentence)
{
  std::stringstream ss(sentence);
  std::string to;

  if (sentence != NULL)
  {
    while(std::getline(ss,to,'\n')){
      cout << to <<endl;
    }
  }

return 0;
}

【讨论】:

  • 还有一个问题。我不确定是否需要关闭字符串流。
  • 它是局部变量,如果超出doSegment会自行销毁。
【解决方案3】:
#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>
    
using namespace std;


vector<string> splitter(string in_pattern, string& content){
    vector<string> split_content;

    regex pattern(in_pattern);
    copy( sregex_token_iterator(content.begin(), content.end(), pattern, -1),
    sregex_token_iterator(),back_inserter(split_content));  
    return split_content;
}
    
int main()
{   

    string sentence = "This is the first line\n";
    sentence += "This is the second line\n";
    sentence += "This is the third line\n";

    vector<string> lines = splitter(R"(\n)", sentence);

    for (string line: lines){cout << line << endl;}

}   
  1. 我们有一个包含多行的字符串
  2. 我们将它们拆分成一个数组(向量)
  3. 我们在 for 循环中打印出这些元素

【讨论】:

  • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
【解决方案4】:

这种相当低效的方法只是遍历字符串,直到遇到 \n 换行符。然后它创建一个子字符串并将其添加到一个向量中。

std::vector<std::string> Loader::StringToLines(std::string string)
{
    std::vector<std::string> result;
    std::string temp;
    int markbegin = 0;
    int markend = 0;

    for (int i = 0; i < string.length(); ++i) {     
        if (string[i] == '\n') {
            markend = i;
            result.push_back(string.substr(markbegin, markend - markbegin));
            markbegin = (i + 1);
        }
    }
    return result;
}

【讨论】:

  • 没有正确处理尾随换行符
【解决方案5】:
#include <sstream>
#include <string>
#include <vector>

std::vector<std::string> split_string_by_newline(const std::string& str)
{
    auto result = std::vector<std::string>{};
    auto ss = std::stringstream{str};

    for (std::string line; std::getline(ss, line, '\n');)
        result.push_back(line);

    return result;
}

【讨论】:

  • 无论有没有 C++ 11,此代码都无法工作。“错误:'std::stringstream' {aka 'std::__cxx11::basic_stringstream'}| 的初始化程序太多”
  • 您可以在这里查看godbolt.org/z/3axvrx 是否有效。
  • @dan-donnelly,我也有这个错误。原来我忘了#include .
猜你喜欢
  • 2012-07-07
  • 2011-08-09
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 2020-11-22
  • 1970-01-01
  • 2017-03-25
相关资源
最近更新 更多