我正在处理一个字符串,并尝试在它不是字母数字(不是 a-z、A-Z 和 0-9)时将其分解。我发现 isalnum 是一个有用的函数。
例如,如果我有字符串“bob-michael !#mi%@pa hi3llary-tru1mp”
vector 应包含:bob、michael、mi、pa、hi3llary 和 tru1mp。
我当前的代码是:
vector<string> result;
string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
stringstream pie(something);
//not sure what to do after this point(I know, not a lot. See below for my current thinking)
我的想法是使用一个循环,当 isalnum 结果为 1 时继续向前,如果 isalnum 结果为 0,则将我目前拥有的任何内容插入字符串 vector 。也许我可以使用 isalnum 作为 delim?我很难接受我的想法并写下这篇文章。谁能指出我正确的方向?谢谢!
编辑:感谢大家的帮助。
最佳答案
也许是这样的:
std::vector<std::string> result;
std::string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
std::regex token("[A-Za-z0-9]+");
std::copy(
std::sregex_token_iterator(something.begin(), something.end(), token),
std::sregex_token_iterator(),
std::back_inserter(result));
关于c++ - 用isalnum拆分字符串并存储到字符串 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540980/