用isalnum拆分字符串并存储到字符串向量中
Splitting a string withisalnum and store into a vector of strings
我正在处理一个字符串,并尝试在它不是字母数字(不是 a-z、A-Z 和 0-9)时将其分解。我发现 isalnum 是一个有用的函数。
例如,如果我有字符串 "bob-michael !#mi%@pa hi3llary-tru1mp"
矢量应包含: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,则将我到目前为止的任何内容推入字符串向量。也许我可以使用 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));
你也可以遍历字符串,然后检查当前索引是否为字母,如果不是则将其分解然后存储到向量
std::string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
std::vector<std::string> result;
std::string newResult = "";
for ( int a = 0; a < something.size(); a++ )
{
if((something[a] >= 'a' && something[a] <= 'z')||(something[a] >= 'A' && something[a] <= 'Z')
|| (something[a] >= '0' && something[a] <= '9'))
{
newResult += something[a];
}
else
{
if(newResult.size() > 0)
{
result.push_back(newResult);
newResult = "";
}
}
}
result.push_back(newResult);
std::replace_if
trick I commented on turned out to not be quite as trivial as I thought it was because std::isalnum
没有 return bool
。
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>
#include <sstream>
#include <iterator>
int main()
{
std::vector<std::string> result;
std::string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
// I expected replace_if(something.begin(), something.end(), &isalnum, " ");
// would work, but then I did a bit of reading and found is alnum returned int,
// not bool. resolving this by wrapping isalnum in a lambda
std::replace_if(something.begin(),
something.end(),
[](char val)->bool {
return std::isalnum(val) == 0;
},
' ');
std::stringstream pie(something);
// read stream into vector
std::copy(std::istream_iterator<std::string>(pie),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string>>(result));
// prove it works
for(const std::string & str: result)
{
std::cout << str << std::endl;
}
}
我正在处理一个字符串,并尝试在它不是字母数字(不是 a-z、A-Z 和 0-9)时将其分解。我发现 isalnum 是一个有用的函数。
例如,如果我有字符串 "bob-michael !#mi%@pa hi3llary-tru1mp"
矢量应包含: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,则将我到目前为止的任何内容推入字符串向量。也许我可以使用 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));
你也可以遍历字符串,然后检查当前索引是否为字母,如果不是则将其分解然后存储到向量
std::string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
std::vector<std::string> result;
std::string newResult = "";
for ( int a = 0; a < something.size(); a++ )
{
if((something[a] >= 'a' && something[a] <= 'z')||(something[a] >= 'A' && something[a] <= 'Z')
|| (something[a] >= '0' && something[a] <= '9'))
{
newResult += something[a];
}
else
{
if(newResult.size() > 0)
{
result.push_back(newResult);
newResult = "";
}
}
}
result.push_back(newResult);
std::replace_if
trick I commented on turned out to not be quite as trivial as I thought it was because std::isalnum
没有 return bool
。
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <algorithm>
#include <sstream>
#include <iterator>
int main()
{
std::vector<std::string> result;
std::string something = "bob-michael !#mi%@pa hi3llary-tru1mp";
// I expected replace_if(something.begin(), something.end(), &isalnum, " ");
// would work, but then I did a bit of reading and found is alnum returned int,
// not bool. resolving this by wrapping isalnum in a lambda
std::replace_if(something.begin(),
something.end(),
[](char val)->bool {
return std::isalnum(val) == 0;
},
' ');
std::stringstream pie(something);
// read stream into vector
std::copy(std::istream_iterator<std::string>(pie),
std::istream_iterator<std::string>(),
std::back_inserter<std::vector<std::string>>(result));
// prove it works
for(const std::string & str: result)
{
std::cout << str << std::endl;
}
}