如何通过分隔符将字符串拆分为数组?

Bab*_*ker 9 c++ string function

我是编程新手.我一直在尝试用C++编写一个函数,它将字符串的内容分解为给定参数的字符串数组,例如:

string str = "___this_ is__ th_e str__ing we__ will use__";
Run Code Online (Sandbox Code Playgroud)

应该返回字符串数组:

cout << stringArray[0]; // 'this'
cout << stringArray[1]; // ' is'
cout << stringArray[2]; // ' th'
cout << stringArray[3]; // 'e str'
cout << stringArray[4]; // 'ing we'
cout << stringArray[5]; // ' will use'
Run Code Online (Sandbox Code Playgroud)

我可以很好地对字符串进行标记,但对我来说最困难的部分是如何在为其指定当前字符串toke之前指定stringArray中的元素数量以及如何从函数返回stringArray.

有人会告诉我如何写这个功能吗?

Edit1: 我不一定需要结果在字符串数组中只是任何容器,我可以调用它作为常规变量与某种索引.

Eri*_*ner 13

这是我第一次尝试使用向量和字符串:

vector<string> explode(const string& str, const char& ch) {
    string next;
    vector<string> result;

    // For each character in the string
    for (string::const_iterator it = str.begin(); it != str.end(); it++) {
        // If we've hit the terminal character
        if (*it == ch) {
            // If we have some characters accumulated
            if (!next.empty()) {
                // Add them to the result vector
                result.push_back(next);
                next.clear();
            }
        } else {
            // Accumulate the next character into the sequence
            next += *it;
        }
    }
    if (!next.empty())
         result.push_back(next);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

希望这能让你对如何解决这个问题有所了解.在您的示例字符串上,它使用此测试代码返回正确的结果:

int main (int, char const **) {
    std::string blah = "___this_ is__ th_e str__ing we__ will use__";
    std::vector<std::string> result = explode(blah, '_');

    for (size_t i = 0; i < result.size(); i++) {
        cout << "\"" << result[i] << "\"" << endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

  • 最后一个元素被忽略了!我在下面提出了一个解决方案,它有效! (2认同)

Mar*_*ork 9

使用STL(抱歉没有编译器未测试)

#include <vector>
#include <string>
#include <sstream>

int main()
{
    std::vector<std::string>   result;

    std::string str = "___this_ is__ th_e str__ing we__ will use__";

    std::stringstream  data(str);

    std::string line;
    while(std::getline(data,line,'_'))
    {
        result.push_back(line); // Note: You may get a couple of blank lines
                                // When multiple underscores are beside each other.
    }
}
Run Code Online (Sandbox Code Playgroud)

//或定义一个标记

#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <sstream>

struct Token: public std::string  // Yes I know this is nasty.
{                                 // But it is just to demosntrate the principle.    
};

std::istream& operator>>(std::istream& s,Token& t)
{
    std::getline(s,t,'_');

    // *** 
    // Remove extra '_' characters from the stream.
    char c;
    while(s && ((c = s.get()) != '_')) {/*Do Nothing*/}
    if (s)
    {
        s.unget(); // Put back the last char as it is not '_'
    }
    return s;
}

int main()
{   
    std::vector<std::string>   result;

    std::string str = "___this_ is__ th_e str__ing we__ will use__";

    std::stringstream  data(str);

    std::copy(std::istream_iterator<Token>(data),
              std::istream_iterator<Token>()
              std::back_inserter(result)
             );
}
Run Code Online (Sandbox Code Playgroud)

  • 这是对用户的练习。 (2认同)