memmem()STL方式?

jac*_*hab 4 c++ stl

有没有一个STL算法可以用来搜索缓冲区内的字节序列,就像memmem()那样?

Has*_*kun 6

我不知道这是不是很好的代码,但以下工作,使用std::search:

#include <cstdio>
#include <string.h>
#include <algorithm>

int main(int argc, char **argv)
{
    char *a = argv[0];
    char *a_end = a + strlen(a);
    char *match = "out";
    char *match_end = match+strlen(match); // If match contained nulls, you would have to know its length.

    char *res = std::search(a, a_end, match, match_end);

    printf("%p %p %p\n", a, a_end, res);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)