gpt4 book ai didi

c++ - 使用 C++11 拆分字符串

转载 作者:IT老高 更新时间:2023-10-28 13:58:46 27 4
gpt4 key购买 nike

使用 c++11 分割字符串最简单的方法是什么?

我见过这个post使用的方法,但我觉得使用新标准应该有一种不那么冗长的方式。

编辑:我想要一个 vector<string>因此,并且能够分隔单个字符。

最佳答案

std::regex_token_iterator基于正则表达式执行通用标记化。对单个字符进行简单拆分可能会也可能不会过大,但它有效并且不太冗长:

std::vector<std::string> split(const string& input, const string& regex) {
// passing -1 as the submatch index parameter performs splitting
std::regex re(regex);
std::sregex_token_iterator
first{input.begin(), input.end(), re, -1},
last;
return {first, last};
}

关于c++ - 使用 C++11 拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9435385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com