C++ 中的字符串分词器允许多个分隔符

发布于 2024-08-29 07:51:49 字数 170 浏览 6 评论 0原文

有没有办法用多个分隔符来标记 C++ 中的字符串?在 C# 中我会这样做:

string[] tokens = "adsl, dkks; dk".Split(new [] { ",", " ", ";" }, StringSplitOptions.RemoveEmpty);

Is there a way to tokenize a string in C++ with multiple separators? In C# I would have done:

string[] tokens = "adsl, dkks; dk".Split(new [] { ",", " ", ";" }, StringSplitOptions.RemoveEmpty);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-09-05 07:51:49

使用 boost::tokenizer。它支持多个分隔符。

事实上,您甚至不需要 boost::tokenizer。如果您想要的只是拆分,请使用 boost::split。该文档有一个示例:
http://www.boost.org/ doc/libs/1_42_0/doc/html/string_algo/usage.html#id1718906

Use boost::tokenizer. It supports multiple separators.

In fact, you don't really even need boost::tokenizer. If all you want is a split, use boost::split. The documentation has an example:
http://www.boost.org/doc/libs/1_42_0/doc/html/string_algo/usage.html#id1718906

伴我心暖 2024-09-05 07:51:49

像这样的事情会做:

void tokenize_string(const std::string &original_string, const std::string &delimiters, std::vector<std::string> *tokens)
{
        if (NULL == tokens) return;

        size_t pos_start = original_string.find_first_not_of(delimiters);
        size_t pos_end   = original_string.find_first_of(delimiters, pos_start);

        while (std::string::npos != pos_start)
        {
                tokens->push_back(original_string.substr(pos_start, pos_end - pos_start));
                pos_start = original_string.find_first_not_of(delimiters, pos_end);
                pos_end   = original_string.find_first_of(delimiters, pos_start);
        }
}

Something like that will do:

void tokenize_string(const std::string &original_string, const std::string &delimiters, std::vector<std::string> *tokens)
{
        if (NULL == tokens) return;

        size_t pos_start = original_string.find_first_not_of(delimiters);
        size_t pos_end   = original_string.find_first_of(delimiters, pos_start);

        while (std::string::npos != pos_start)
        {
                tokens->push_back(original_string.substr(pos_start, pos_end - pos_start));
                pos_start = original_string.find_first_not_of(delimiters, pos_end);
                pos_end   = original_string.find_first_of(delimiters, pos_start);
        }
}
向地狱狂奔 2024-09-05 07:51:49

这是我的版本(尚未经过严格测试):

std::vector<std::string> split(std::string const& s,
    std::vector<std::string> const& delims)
{
    std::vector<std::string> parts;

    std::vector<std::pair<std::string::size_type, std::string::size_type>> poss;
    poss.reserve(delims.size());

    std::string::size_type beg = 0;

    for(;;)
    {
        poss.clear();

        std::string::size_type idx = 0;
        for(auto const& delim: delims)
        {
            if(auto end = s.find(delim, beg) + 1)
                poss.emplace_back(end - 1, idx);
            ++idx;
        }

        if(poss.empty())
            break;

        std::sort(std::begin(poss), std::end(poss));

        auto old_beg = beg;

        for(auto pos: poss)
        {
            parts.emplace_back(std::begin(s) + beg,
                std::begin(s) + old_beg + pos.first);
            beg = pos.first + delims[pos.second].size();
        }
    }

    if(beg < s.size())
        parts.emplace_back(std::begin(s) + beg, std::end(s));

    return parts;
}

Here is my version (not heavily tested (yet)):

std::vector<std::string> split(std::string const& s,
    std::vector<std::string> const& delims)
{
    std::vector<std::string> parts;

    std::vector<std::pair<std::string::size_type, std::string::size_type>> poss;
    poss.reserve(delims.size());

    std::string::size_type beg = 0;

    for(;;)
    {
        poss.clear();

        std::string::size_type idx = 0;
        for(auto const& delim: delims)
        {
            if(auto end = s.find(delim, beg) + 1)
                poss.emplace_back(end - 1, idx);
            ++idx;
        }

        if(poss.empty())
            break;

        std::sort(std::begin(poss), std::end(poss));

        auto old_beg = beg;

        for(auto pos: poss)
        {
            parts.emplace_back(std::begin(s) + beg,
                std::begin(s) + old_beg + pos.first);
            beg = pos.first + delims[pos.second].size();
        }
    }

    if(beg < s.size())
        parts.emplace_back(std::begin(s) + beg, std::end(s));

    return parts;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文