• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

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

如果要从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

posted @ 2013-09-21 13:11  Class Xman  阅读(523)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3