c++ - boost::algorithm - 拆分字符串返回一个额外的标记

标签 c++ string boost

也许有人可以告诉我这里发生了什么?

我的意图是在大括号上拆分输入字符串:即:'(' 或 ')'。

对于 "(well)hello(there)world" 的输入字符串,我希望返回 4 个标记:well;你好;那里;世界

正如您从下面我的示例应用程序中看到的那样,我得到了 5 个 token (第一个是空字符串)。

有什么方法可以让它只返回非空字符串吗?

#include <iostream>
#include <boost/algorithm/string.hpp>
#include <vector>
int main()
{
    std::string in = "(well)hello(there)world";

    std::vector<std::string> tokens;
    boost::split(tokens, in, boost::is_any_of("()"));

    for (auto s : tokens)
        std::cout << "\"" << s << "\"" << std::endl;

    return 0;
}

输出:

$ a.out
""        <-- where is this token coming from?
"well"
"hello"
"there"
"world"

我已经尝试使用 boost::algorithm::token_compress_on 但我得到了相同的结果。

最佳答案

是的,返回的第一个结果是第一个左括号前的空集 {}。行为符合预期。

如果您不想使用该结果,只需测试一个空的返回变量并将其丢弃。

要测试这是预期的行为,请在该行的末尾放置一个括号,您将在末尾得到另一个空结果。 :)

关于c++ - boost::algorithm - 拆分字符串返回一个额外的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612965/

相关文章:

c++ - 刷新 QGraphicsScene/QGraphicsView

c++ - 响应 EN_UPDATE 消息时避免递归

c++ - xcode 中的 boost 错误

c - 函数和字符串,检查输入字符串是否匹配

c++ - Boost::multi_array 性能问题

c++ - boost::xpressive 查看序列的开头

python - 定义像 Python "with"语句一样工作的 C 宏有什么缺点?

c++ - 如何使用 get line() 从文件中读取间隔字符串?

javascript - 如何将数组添加到字符串而不在 JS 中展平它们?

java - 如何优化这个正则表达式?