memmem()STL方式?

时间:2010-07-19 11:15:44

标签: c++ stl

是否有STL算法可用于搜索缓冲区内的字节序列,如memmem()吗?

4 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

std::search会在另一个序列中找到第一个出现的序列。

答案 2 :(得分:0)

findsubstr呢?

#include <string>
using std::string;
...
size_t found;

found = s.find("ab",4);
if (found != string::npos)
  finalString = s.substr(found); // get from "ab" to the end

答案 3 :(得分:-1)

为什么不使用strstr?

没有实施算法。您可以实现自己的谓词与std :: find_if结合使用,但它是过度工程,IMO。