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 << '
';
  if (it!=haystack.end())
    cout << "The first match is: " << *it << '
';

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

  return 0;
}
原文地址:https://www.cnblogs.com/sea-stream/p/9823594.html