C ++逐行拆分

时间:2012-11-01 06:37:44

标签: c++ split

我需要逐行拆分。 我以前用以下方式做的事情:

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

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

5 个答案:

答案 0 :(得分:47)

我想使用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;
}

答案 1 :(得分:13)

您可以在循环中调用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

答案 2 :(得分:1)

#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) We have a string with multiple lines
//  2) we split those into an array (vector)
//  3) We print out those elements in a for loop


//  My Background. . .  
//  github.com/Radicalware  
//  Radicalware.net  
//  https://www.youtube.com/channel/UCivwmYxoOdDT3GmDnD0CfQA/playlists  

答案 3 :(得分:0)

这种相当低效的方法只是循环遍历字符串,直到遇到\ 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;
}

答案 4 :(得分:0)

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