在我的项目中,我需要从索引0开始迭代一个大字符串,并获取长度为k的子字符串。我已经实现了string::substr(),但想知道是否有其他高效的方法。
例如:
例如:
std::string S ="ABCDEFGHIJKLMN"
我需要获得从字符串S的开头开始长度为5的所有子串。就像"ABCDE"
,"BCDEF"
,"CDEFG"
等一样。
我的实现如下:
void geekfunc(std::string &str)
{
unsigned int index=0;
for (; index<=(str.size()-K);++index)
{
++myseqmap[str.substr(index,K)];
}
}
这个函数被调用了一千万次,欢迎尝试其他方法。
k
的子串吗? - Rerito