Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

find_first_of

函数模板  <algorithm>
C++98 equality (1)
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2);
predicate(2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2,
                                   BinaryPredicate pred);

从设置的范围查找元素

返回一个迭代器,指向[first1,last1)范围内----并且匹配[first2,last2)中的任何元素的第一个元素。 如果没有找到这样的元素,函数返回last1。
使用operator==(或版本(2)中的pred)将[first1,last1) 中的元素与[first2,last2)中的每个值进行顺序比较,直到匹配为止。

这个函数模板的行为相当于:
template<class InputIterator, class ForwardIterator>
  InputIterator find_first_of ( InputIterator first1, InputIterator last1,
                                ForwardIterator first2, ForwardIterator last2)
{
  while (first1!=last1) {
    for (ForwardIterator it=first2; it!=last2; ++it) {
      if (*it==*first1)          // or: if (pred(*it,*first)) for version (2)
        return first1;
    }
    ++first1;
  }
  return last1;
}

☲  参数


first1, last1
C++98: 指向序列初始和最终位置的前向迭代器。使用的范围是[first1,last1], 它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。 C++11: 指向序列初始和最终位置的输入迭代器。使用的范围是[first1,last1], 它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
first2, last2
指向序列初始和最终位置的前向迭代器,使用的范围是[first2,last2)。
对于(1),两个范围内的元素都必须具有使用operator==可比较的类型 (第一个范围内的元素作为左侧操作数,第二个范围内的元素作为右侧操作数)。
pred
接受两个元素作为参数(这两个序列中每个元素一个,顺序相同), 并返回可转换为bool的值的二进制函数。返回的值表明是否认为元素在该函数的当前环境中匹配。
函数不能修改它的任何参数。
它可以是函数指针,也可以是函数对象。

☉  返回值



指向[first1,last1)中第一个元素的迭代器,它是[first2,last2)的一部分。
如果没有匹配的元素,函数返回last1。
C++98:
如果[first2,last2)是一个空范围,则结果是未指定的。
C++11:
如果[first2,last2)是一个空范围,函数返回last1。

☣  示例



// find_first_of example
#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';

  return 0;
}

输出:
The first match is: A
The first match is: a

✥ 复杂度



接近count1*count2(countX是firstX和lastX之间的 distance):比较元素直到找到匹配。

⇄ 数据竞争


两个范围内的部分(或全部)对象被访问 (在[first1,last1的情况下最多访问一次),在[first2,last2)中可能访问不止一次)。

☂ 异常安全性



如果任何元素比较(或pred)或迭代器上的任何操作抛出,则抛出。
注意,无效的参数会导致未定义的行为。

🍄  另请参阅



search 搜索子序列的范围(函数模板)
find 在范围内查找值(函数模板)
find_if 在范围内查找元素(函数模板)

联系我们 免责声明 关于CandCplus 网站地图