C++ 如何在std::vector中查找模式

C++ 如何在std::vector中查找模式,c++,vector,c++14,stdvector,C++,Vector,C++14,Stdvector,是否有任何直接的方法可以找到std::vector容器中是否存在一组特定的值(模式) 假设我有一个数据容器: std::vector<int> data { 0x00, 0xff, 0x00, 0x11, 0x12, 0x13, 0x14, 0x15 }; 我想: 表示模式存在的布尔值 最终,模式开始的索引 您可以使用std::search #include <iostream> #include <vector> #include <algorit

是否有任何直接的方法可以找到
std::vector
容器中是否存在一组特定的值(模式)

假设我有一个数据容器:

std::vector<int> data { 0x00, 0xff, 0x00, 0x11, 0x12, 0x13, 0x14, 0x15 };
我想:

  • 表示模式存在的布尔值

  • 最终,模式开始的索引


您可以使用
std::search

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> data {0x00, 0xff, 0x00, 0x11, 0x12, 0x13, 0x14, 0x15};
    std::vector<int> pattern {0x00, 0xff, 0x00};

    auto res = std::search(std::begin(data), std::end(data), std::begin(pattern), std::end(pattern));
    if(res == std::end(data)) {
        std::cout << "Couldn't find it.\n";
    } else {
        std::cout << "Found it.\n";
    }
}
#包括
#包括
#包括
int main(){
向量数据{0x00,0xff,0x00,0x11,0x12,0x13,0x14,0x15};
向量模式{0x00,0xff,0x00};
auto res=std::search(std::begin(数据)、std::end(数据)、std::begin(模式)、std::end(模式));
如果(res==std::end(数据)){

std::cout可能带有lambda和
std::find()
的内容。请显示输入和期望的输出。@虽然我们已经获得了输入和期望的输出?请看一下。@bilzeian“为什么我得到三张反对票?”提供一个答案来改进你的问题,而不是问独角兽或小马。没有人真正对你想要什么感兴趣。
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> data {0x00, 0xff, 0x00, 0x11, 0x12, 0x13, 0x14, 0x15};
    std::vector<int> pattern {0x00, 0xff, 0x00};

    auto res = std::search(std::begin(data), std::end(data), std::begin(pattern), std::end(pattern));
    if(res == std::end(data)) {
        std::cout << "Couldn't find it.\n";
    } else {
        std::cout << "Found it.\n";
    }
}