boost::split: how to split a string with a character?
I'm stuck with this simple problem. Say I have a string which is consisted from characters [0-9]. What I开发者_JAVA技巧 would like to do is split the string by the single characters using boost::split.
std::string str = "0102725";
std::vector<std::string> str2;
boost::split(str2, str, boost::is_any_of(SOMETHING));
I'm looking for SOMETHING so that str2[0] contains "0", str2[1] contains "1", str2[2] contains "0" and so on. So far I have tried "", ':' and ":" but no luck...
boost::split
is overkill for that.
for (size_t i=0; i < str.length(); i++)
str2.push_back(std::string(1, str.at(i)));
Apart from @Mat's approach, here is my take on it. Since you are separating characters you may not want,
vector<string> str2; // acquires character + extra space for '\0'
But rather,
vector<char> str2; // only single character
Here is how you can do it alternatively:
for(unsigned int i = 0; i < str.size(); i++)
str2.push_back(str[i]);
Demo. You can access str2[i]
when you want.
精彩评论