/*
find_first_of算法带有两对迭代器参数,来标记两段元素范围,在第一段范围中
查找与第二段范围中任意元素匹配的元素,返回一个迭代器,指向第一个匹配的元素。
如果找不到则返回第一个元素的end迭代器。
*/
/*
template <class InputIterator, class ForwardIterator>
ForwardIterator1 find_first_of (InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2);
predicate (2) template <class InputIterator, class ForwardIterator, class BinaryPredicate>
ForwardIterator1 find_first_of (InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2,
BinaryPredicate pred);
*/
#include <iostream> // std::cout
#include <algorithm> // std::find_first_of
#include <vector> // std::vector
#include <cctype> // std::tolower
bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
}
int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+6);
std::vector<char>::iterator it;
int needle[] = {'A','B','C'};
// using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
// using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+3, comp_case_insensitive);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
system("pause");
return 0;
}
/*
Output:
The first match is: A
The first match is: a
请按任意键继续. . .
*/
find_first_of
最新推荐文章于 2024-08-23 11:15:23 发布