c++中有很多种分割字符串的方法,哪种方法性能最好?
as suggested by this post:
Split a string in C++?
there are many ways to split a string, but whose performance is best ?
is there any benchmark on this test ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了最大限度地提高 Very Fast Implement™ 的机会,您应该使用恒定时间的子字符串创建操作。一种方法是确保原始字符串存在并且在子字符串引用的生命周期内不会被修改。然后,您可以将每个子字符串表示为例如两个指针,或者一个指针和一个长度,或者适合特定上下文的任何内容。
对于一个不寻常的上下文示例,它允许可能令人惊讶的子字符串表示,当原始字符串是可丢弃的并且子字符串可以是 C 样式以零结尾的字符串时,您可以将原始字符串中的子字符串分隔符替换为空字节,然后子字符串可以被表示为例如单个指针。
不管怎样,最后你只需要测量。
To maximize your chance of a Very Fast Implementation™ you should use a substring creation operation that has constant time. One way to do that is to ensure that the original string exists and is not modified over the lifetime of the substring references. You can then represent each substring as e.g. two pointers, or as a pointer and a length, or as whatever suits the particular context.
For an unusual context example that allows a perhaps surprising substring representation, when the original string is discardable and the substrings can be C style zero-terminated strings,then you can replace substring delimiters in the original string with null bytes, and then a substring can be represented as e.g. a single pointer.
Anyway, in the end you will just have to MEASURE.