chr*_*irk 1 c++ string templates find
字符串的平均长度为4个字符.我认为二进制搜索可能是从第4位开始最快的.另外我认为内联模板化函数可能表现良好.这是在一个非常紧凑的循环中完成的,因此性能至关重要.
数据看起来像:
"1234 "
"ABC "
"A1235 "
"A1235kgo"
Run Code Online (Sandbox Code Playgroud)
seh*_*ehe 10
char* found = std::find(arr, arr+9, ' ');
Run Code Online (Sandbox Code Playgroud)
请注意,结束迭代器发出'不匹配'的信号:
bool match = (arr+9) != found;
Run Code Online (Sandbox Code Playgroud)
注意
-O3 -march=native对于g ++)编辑,因为你已经显示了更多的代码,我现在意识到你实际上想要检测(子)字符串长度.你可以用
当然,假设您希望将char []转换为std :: string用于此目的.实际上,这可能是一个非常有效的想法,因为在几乎所有C++标准库的实现中都发现了SSO(小字符串优化).(参见Herb Sutter的更多例外C++中的第 13-16项,或Scott Meyers关于Effective STL中商业std :: string实现的讨论).