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

std::

find_end

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

求范围内的最后一个子序列

在[first1,last1)范围内搜索由[first2,last2)定义的序列的最后一次出现, 并返回指向其第一个元素的迭代器,如果没有发现出现则返回last1。 使用operator==(或版本(2)中的pred)顺序比较两个范围内的元素: 只有当[first1,last1]的所有元素都匹配时,[first2,last2]的子序列才被认为匹配。 这个函数返回最后一次出现的序列。有关返回第一个的算法,请参阅search。

这个函数模板的行为相当于:
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2, ForwardIterator2 last2)
{
  if (first2==last2) return last1;  // specified in C++11

  ForwardIterator1 ret = last1;

  while (first1!=last1)
  {
    ForwardIterator1 it1 = first1;
    ForwardIterator2 it2 = first2;
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version (2)
        ++it1; ++it2;
        if (it2==last2) { ret=first1; break; }
        if (it1==last1) return ret;
    }
    ++first1;
  }
  return ret;
}

☲  参数


first1, last1
指向序列初始和最终位置的前向迭代器。使用的范围是[first1,last1], 它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。

first2, last2
指向序列初始和最终位置的前向迭代器。使用的范围是[first2,last2], 它包含first2和last2之间的所有元素,包括first2指向的元素,但不包括last2指向的元素。
对于(1),两个范围内的元素都必须具有使用operator==可比较的类型 (第一个范围内的元素作为左侧操作数,第二个范围内的元素作为右侧操作数)。

pred
接受两个元素作为参数(这两个序列中各一个元素,顺序相同), 并返回可转换为bool的值的二进制函数 返回的值表明该元素在该函数当前环境中是否匹配。
函数不应修改其参数。
它可以是函数指针,也可以是函数对象。

☉  返回值



指向[first1,last1]中[first2,last2)最后一次出现的第一个元素的迭代器。
如果没有找到序列,函数返回last1。
C++98
如果[first2,last2)是一个空范围,则结果是未指定的。
C++11
如果[first2,last2)是一个空范围,函数返回last1。

☣  示例



// find_end example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vector

bool myfunction (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {1,2,3,4,5,1,2,3,4,5};
  std::vector<int> haystack (myints,myints+10);

  int needle1[] = {1,2,3};

  // using default comparison:
  std::vector<int>::iterator it;
  it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);

  if (it!=haystack.end())
    std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';

  int needle2[] = {4,5,1};

  // using predicate comparison:
  it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);

  if (it!=haystack.end())
    std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';

  return 0;
}

输出:
needle1 last found at position 5
needle2 last found at position 3

✥ 复杂度



count2*(1+count1-count2)中线性,其中countX是firstX和lastX之间的distance: 比较元素,直到找到最后一个匹配的子序列。

⇄ 数据竞争


两个范围内的部分(或全部)对象被访问(可能不止一次)。

☂ 异常安全性



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

🍄  另请参阅



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

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