从vector容器中查找一个子串:search()算法

时间:2021-06-21 06:41:04

如果要从vector容器中查找是否存在一个子串序列,就像从一个字符串中查找子串那样,次数find()与find_if()算法就不起作用了,需要采用search()算法:例子:

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int_tmain(int argc, _TCHAR* argv[])

{

vector<char> targetVec;

vector<char> sourchVec;

targetVec.push_back('\0');

targetVec.push_back('2');

sourchVec.push_back('3');

sourchVec.push_back('1');

sourchVec.push_back('\0');

sourchVec.push_back('\0');

sourchVec.push_back('2');

vector<char>::iterator posIt;

posIt= search(sourchVec.begin(),sourchVec.end(),targetVec.begin(),targetVec.end());

if(posIt != sourchVec.end())

{

cout<<"find it"<<endl;

}

return 0;

}

执行结果:find it