C++非修改序列操作—查找(find;fnd_first_of;find_if;find_if_not;find_end;adjacent_if)

一、find

头文件algorithm

template <class InputIterator, class T>
   InputIterator find (InputIterator first, InputIterator last, const T& val);

查找范围内的值

返回范围[first,last]中与val相等的第一个元素的迭代器。 如果找不到这样的元素,则该函数返回最后。

该函数使用operator ==将各个元素与val进行比较。

此函数模板的行为等效于:

template<class InputIterator, class T>
  InputIterator find (InputIterator first, InputIterator last, const T& val)
{
  while (first!=last) {
    if (*first==val) return first;
    ++first;
  }
  return last;
}

参数

  1. first,last:
    将迭代器输入到序列中的初始位置和最终位置。 搜索的范围是[first,last),其中包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. val:
    要在范围内搜索的值。
    T应该是一种支持与InputIterator指向的元素进行比较的类型,使用operator ==(元素为左侧操作数,val为右侧)。

返回值

等于val的范围中第一个元素的迭代器。如果没有元素匹配,则函数最后返回。

// find example
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector
#include <list>         // std::list
int main () {
	// using std::find with array and pointer:
	int myints[] = { 10, 20, 30, 40 };
	int * p;

	p = std::find (myints, myints+4, 30);
	if (p != myints+4)
		std::cout << "Element found in myints: " << *p << '\n';
	else
		std::cout << "Element not found in myints\n";

	// using std::find with vector and iterator:
	std::vector<int> myvector (myints,myints+4);
	std::vector<int>::iterator it;

	it = find (myvector.begin(), myvector.end(), 30);
	if (it != myvector.end())
		std::cout << "Element found in myvector: " << *it << '\n';
	else
		std::cout << "Element not found in myvector\n";

	// using std::find with vector and iterator:
	std::list<int> mylist (myints,myints+4);
	std::list<int>::iterator is;
	is = find (mylist.begin(), mylist.end(), 30);
	if (is != mylist.end())
		std::cout << "Element found in mylist: " << *is << '\n';
	else
		std::cout << "Element not found in mylist\n";

	return 0;
}

在这里插入图片描述
如果需要传递一个子区间,则传递指向这个子区间的第一个元素以及最后一个元素的下一位置的迭代器(或指针)。

例如,在下面对find函数的调用中,只搜索了ia[1]和ia[2]:

//only search elements ia[1] and ia[2]
int *result = find(ia + 1 , ia + 3 , search_value);

二、find_first_of

头文件algorithm

从范围内的集合中查找元素

返回[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;
}

参数

  1. first1,last1
    【C++ 98】将迭代器转发到搜索序列的初始和最终位置。使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
    【C++ 11】将迭代器输入到搜索序列的初始位置和最终位置。使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。

  2. first2,last2
    将迭代器转发到要搜索的元素值的初始位置和最终位置。使用的范围是[first2,last2)。对于(1),两个范围中的元素应具有可比较的类型,使用operator ==(第一个范围的元素作为左侧操作数,第二个元素的元素作为右侧操作数)。

  3. 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;
}

在这里插入图片描述

三、find_if

头文件algorithm

template <class InputIterator, class UnaryPredicate>
   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

查找范围内的元素

返回到[first,last]范围内第一个元素的迭代器,其中pred返回true。 如果找不到这样的元素,则该函数返回最后。

此函数模板的行为等效于:

template<class InputIterator, class UnaryPredicate>
  InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (pred(*first)) return first;
    ++first;
  }
  return last;
}

参数

  1. first,last
    将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示元素是否在此函数的上下文中被视为匹配。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

对于pred不返回false的范围中的第一个元素的迭代器。如果所有元素的pred为false,则函数返回最后。

find_if算法 是find的一个谓词判断版本,它利用返回布尔值的谓词判断pred,检查迭代器区间[first, last)上的每一个元素,如果迭代器iter满足pred(*iter) == true,表示找到元素并返回迭代器值iter;未找到元素,则返回last。

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

bool IsOdd (int i) {
  return ((i%2)==1);
}

int main () {
  std::vector<int> myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "The first odd value is " << *it << '\n';

  return 0;
}

在这里插入图片描述

例外

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

四.find_if_not

头文件algorithm

查找范围内的元素(负面条件)

返回到[first,last]范围内第一个元素的迭代器,pred返回false。 如果找不到这样的元素,则该函数返回最后。

此函数模板的行为等效于:

template<class InputIterator, class UnaryPredicate>
  InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last) {
    if (!pred(*first)) return first;
    ++first;
  }
  return last;
}

参数

  1. first,last
    将迭代器输入到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数,接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示元素是否在此函数的上下文中被视为匹配。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

pred返回false的范围中第一个元素的迭代器。如果所有元素的pred都为true,则该函数返回最后。

// find_if_not example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if_not
#include <array>        // std::array

int main () {
  std::array<int,5> foo = {1,2,3,4,5};

  std::array<int,5>::iterator it =
    std::find_if_not (foo.begin(), foo.end(), [](int i){return i%2;} );
  std::cout << "The first even value is " << *it << '\n';

  return 0;
}

输出:The first even value is 2

五、find_end

头文件algorithm

查找范围中的最后一个子序列

在范围[first1,last1]中搜索由[first2,last2]定义的序列的最后一次出现,并将迭代器返回到其第一个元素,如果没有找到则出现last1。

使用operator ==(或者版本(2)中的pred)顺序比较两个范围中的元素:[first1,last1]的子序列仅在[first2,last2]的所有元素都为真时才被视为匹配。

此函数返回最后一次出现的事件。 对于返回第一个算法的算法,请参阅搜索。

此函数模板的行为等效于:

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;
}

参数

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

  2. first2,last2
    将迭代器转发到要搜索的序列的初始位置和最终位置。使用的范围是[first2,last2)。对于(1),两个范围中的元素应具有可比较的类型,使用operator ==(第一个范围的元素作为左侧操作数,第二个元素的元素作为右侧操作数)。

  3. pred
    二进制函数,接受两个元素作为参数(两个序列中的每个序列中的一个,顺序相同),并返回一个可转换为bool的值。返回值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

[first1,last1]中[last2,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;
}

在这里插入图片描述

例外

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

六、adjacent_find

头文件algorithm

在范围内找到相等的相邻元素

搜索范围[first,last)以查找匹配的两个连续元素的第一次出现,并将迭代器返回到这两个元素中的第一个,如果没有找到这样的对,则返回最后一个。

如果两个元素使用运算符==(或使用pred,版本(2))进行比较,则两个元素匹配。

此函数模板的行为等效于:

template <class ForwardIterator>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
{
  if (first != last)
  {
    ForwardIterator next=first; ++next;
    while (next != last) {
      if (*first == *next)     // or: if (pred(*first,*next)), for version (2)
        return first;
      ++first; ++next;
    }
  }
  return last;
}

参数

  1. first,last
    将迭代器转发到搜索序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    二进制函数,接受两个元素作为参数,并返回一个可转换为bool的值。 返回值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

[first,last]范围内第一对匹配连续元素的第一个元素的迭代器。如果找不到这样的对,则该函数返回最后。

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

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

int main () {
  int myints[] = {5,20,5,30,30,20,10,10,20};
  std::vector<int> myvector (myints,myints+8);
  std::vector<int>::iterator it;

  // using default comparison:
  it = std::adjacent_find (myvector.begin(), myvector.end());

  if (it!=myvector.end())
    std::cout << "the first pair of repeated elements are: " << *it << '\n';

  //using predicate comparison:
  it = std::adjacent_find (++it, myvector.end(), myfunction);

  if (it!=myvector.end())
    std::cout << "the second pair of repeated elements are: " << *it << '\n';

  return 0;
}

在这里插入图片描述

总结:

find() : 在序列中找某个值的第一个出现
find_if() : 在序列中符合某谓词的第一个元素
find_first_of() : 在两个序列中找匹配元素
find_if_not():查找范围内的元素(负面条件)
find_end():在范围内搜索由另一个序列定义的最后一个元素
adjacent_find() : 用于查找相等或满足条件的邻近元素对

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值