c++ 匹配A容器中最先出现的b容器中的元素,返回iterator,(find_first_of)

 

#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolower
using namespace std;
bool comp_case_insensitive (char c1, char c2) {
  return (tolower(c1)==tolower(c2));
}

int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  vector<char> haystack (mychars,mychars+6);
  vector<char>::iterator it;

  int needle[] = {'C'};

  // using default comparison:
  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
  cout << "The first match is: " << *it << '\n';
  if (it!=haystack.end())
    cout << "The first match is: " << *it << '\n';

  // using predicate comparison:
  it = find_first_of (haystack.begin(), haystack.end(),needle, needle+3, comp_case_insensitive);
  cout << "The first match is: " << *it << '\n';
  if (it!=haystack.end())
    cout << "The first match is: " << *it << '\n';

  return 0;
}

 

posted @ 2018-10-21 00:20  anobscureretreat  阅读(311)  评论(0编辑  收藏  举报