的strstr()在C#中相当于 [英] strstr() equivalent in C#
问题描述
我有两个字节[
],我想找到的第二个第一次出现字节[]
的第一个字节[]
(或一定范围的话)。
我不希望使用效率的字符串(翻译第一字节[]
到字符串
将效率不高)。
基本上,我认为这就是的strstr()
做了C。
什么是做到这一点的最好办法(所以它是有效率的,易于使用)?
这是它应该是什么样子:
INT GetOffsetOfArrayInArray(byte []的bigArray,INT bigArrayOffset,INT bigArrayCount,byte []的smallArray);
谢谢!
更新:
我想一个解决方案,会比一个简单的搜索更高效。这意味着使用的事实,比较缓冲器可以更有效地应使用 - 的 memcmp()比遍历字节更高效即可。
另外,我知道有一些优化像这样的场景算法:
- 大阵:12312351231234
- 小阵:1231234
- 天真的算法: 7比较发现,1231235,是不是1231234不同,2比较去寻找下一个1,4比较发现,1235比不同1231,3相比较,找到下一个1,7比较找到匹配。共有7 + 2 + 4 + 3 + 7 = 23进行比较。
- 智能算法: 7比较发现,1231235,是不是1231234不同,直接跳转到下一个1(不比较),4比较发现,1235是比1231,不同的直接跳跃超越了5,7比较,找到匹配。总共有7 + 4 + 7 = 18进行比较。
我没有任何code你,但最快的解决方案,你会发现的名称是的博耶 - 穆尔算法。它可以比为O(n)做的更好。
这里是在$ C $的CProject字符串的实现。看起来像一个转换字节[]
应该不会太困难。</ P>
I have two byte[]
and I want to find the first occurrence of the second byte[]
in the first byte[]
(or a range in it).
I don't want to use strings for efficiency (translating the first byte[]
to a string
will be inefficient).
Basically I believe that's what strstr()
does in C.
What is the best way to do that (so it be efficient and easy to use)?
This is how it should look like:
int GetOffsetOfArrayInArray(byte[] bigArray, int bigArrayOffset, int bigArrayCount, byte[] smallArray);
Thanks!
UPDATE:
I want a solution that would be more efficient than a simple search. This means that using the fact that comparing buffers can be more efficient should be used - memcmp() is more efficient than iterating over the bytes.
Also, I know there are algorithms that optimize scenarios like this one:
- big array: "12312351231234"
- small array: "1231234"
- Naive algorithm: 7 compares to find that "1231235" is different than "1231234", 2 compares to find the next "1", 4 compares to find that "1235" is different than "1231", 3 compares to find the next "1", 7 compares to find match. A total of 7+2+4+3+7=23 compares.
- Smart algorithm: 7 compares to find that "1231235" is different than "1231234", directly jumps to the next "1" (without comparing), 4 compares to find that "1235" is different than "1231", directly jumps beyond the "5", 7 compares to find the match. A total of 7+4+7=18 compares.
I don't have any code for you but the name of the fastest solution you will find is the Boyer-Moore algorithm. It can do better than O(n).
Here is an implementation for strings on CodeProject. Looks like a conversion to byte[]
should not be too difficult.
这篇关于的strstr()在C#中相当于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!