2012-11-01 44 views
17

我需要按行分割字符串。 我用下面的方法做:“我们是一个\ nyes我们是”C++按行分割字符串

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方法。但是当我调试时,我发现句子参数是“我们是一个。\ \ n我们是”,并且分裂失败了。有人可以告诉我为什么发生这种情况,我该怎么做。无论如何,我还可以用C++分割字符串。谢谢 !

+2

我建议使用真正的C++ - 看看的std ::函数getline –

+1

您需要就输入了一些变化。不是分割功能。 – halfelf

+0

当你修改你传递给它的字符串时,请小心'strtok'。 –

回答

35

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

还有一个问题。我不确定stringstream是否需要关闭。 – wangzhiju

+4

它是本地变量,如果脱离doSegment,它将自行破坏。 – billz

9

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

+3

如果您使用的分隔符具有多个字符,比如我,则需要更改“prev = pos + 1;”行到“prev = pos + delimiter.size();”代替。否则,您将在矢量中的下一个元素的开头留下剩余的字符。 –

0
#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 
+0

虽然此代码片段可能是解决方案,但[包括解释](// meta.stackexchange.com/questions/114762/explaining-entirely-基于代码的答案)确实有助于提高帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – peacetype

+0

如果向下滚动,您将在代码中看到注释。出于某种原因,堆栈溢出不会让我在代码块之外放置文本。 – Scourge

+0

// 1)我们有一个多行字符串 // 2)我们将它们分成一个数组(向量) // 3)我们在for循环中打印出这些元素 – Scourge