我用
// str is a string that holds the line of data from ifs- the text file.
// str holds the words to be split, res the vector to store them in.
while( getline( ifs, str ) )
split(str, res);
void split(const string& str, vector<string>& vec)
{
typedef unsigned int uint;
const string::size_type size(str.size());
uint start(0);
uint range(0);
/* Explanation:
* Range - Length of the word to be extracted without spaces.
* start - Start of next word. During initialization, starts at space 0.
*
* Runs until it encounters a ' ', then splits the string with a substr() function,
* as well as making sure that all characters are lower-case (without wasting time
* to check if they already are, as I feel a char-by-char check for upper-case takes
* just as much time as lowering them all anyway.
*/
for( uint i(0); i < size; ++i )
{
if( isspace(str[i]) )
{
vec.push_back( toLower(str.substr(start, range + 1)) );
start = i + 1;
range = 0;
} else
++range;
}
vec.push_back( toLower(str.substr(start, range)) );
}
我不确定这对您是否特别有帮助,但我会尝试。toLower 函数是一个快速函数,它只使用 ::toLower() 函数。这会读取每个字符直到一个空格,然后将其填充到一个向量中。我不完全确定你对 char by char 的意思。
你想一次提取一个单词字符吗?或者您想在进行过程中检查每个字符?或者你的意思是你想提取一个词,完成,然后回来?如果是这样,我会 1) 无论如何推荐一个向量,以及 2) 让我知道,以便我可以重构代码。