STL algorithm算法adjacent_find(4)

原文地址:http://www.cplusplus.com/reference/algorithm/adjacent_find/
function template
<algorithm>

std::adjacent_find

equality (1)
template <class ForwardIterator>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
predicate (2)
template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
                                  BinaryPredicate pred);
Find equal adjacent elements in range

Searches the range [first,last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.

在范围[first,last)之间寻找第一次出现的两个连续相等的元素,如果存在,则返回指向第一个元素迭代器,否则返回last.

例子:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> vi{0,1,0,2,2,4,5,6};
	auto it=adjacent_find(vi.begin(),vi.end());
	if(it!=vi.end())
		cout<<"some match"<<endl;
	else
		cout<<"mismacht"<<endl;
	cout<<"*it="<<*it<<endl;

}
运行截图:


注意第一个匹配的是2而不是0!是两个连续相等的元素


Two elements match if they compare equal using operator== (or using pred, in version (2)).

如果使用==两个元素相等,那么就匹配。(第二种使用二元断言)

例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool isEqual(int i,int j){
	if(i==j-1)//be care
		return true;
	else
		return false;
	
}
int main()
{
	vector<int> vi{10,9,8,7,5,6};
	auto it=adjacent_find(vi.begin(),vi.end(),isEqual);
	if(it!=vi.end())
		cout<<"some match"<<endl;
	else
		cout<<"mismacht"<<endl;
	cout<<"*it="<<*it<<endl;

}
运行截图:


看清楚我这里的二言断言哦。


The behavior of this function template is equivalent to:

其行为类似如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}



Parameters

first, last

Forward iterators to the initial and final positions of the searched sequence. The range used is [first,last), whichcontains all the elements between first and last, including the element pointed by first but not the elementpointed by last.

要搜索序列的标记范围,包含了fist到last之间的所有元素,包括first所指向的元素,但不包括last指向的元素。


pred
Binary function that accepts two elements as arguments, and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function.
The function shall not modify any of its arguments.

This can either be a function pointer or a function object.

一个接受两个元素类型参数并返回一个bool值的二元函数。

该函数不应该修改其参数。

可以是一个指针或者函数对象。



Return value

An iterator to the first element of the first pair of matching consecutive elements in the range [first,last).
If no such pair is found, the function returns last.

如果匹配返回匹配的指向第一个元素的迭代器,否则返回last.

例子:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> vi{0,1,0,2,3,4,5,6};
	auto it=adjacent_find(vi.begin(),vi.end());
	if(it!=vi.end())
		cout<<"some match"<<endl;
	else
		cout<<"mismacht"<<endl;
	cout<<"*it="<<*it<<endl;

}
运行截图:



Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 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;
}


Output:
the first pair of repeated elements are: 30
the second pair of repeated elements are: 10

Complexity

Up to linear in the distance between first and last: Compares elements until a match is found.

和first,last之间的距离线性相关,比较两个元素直到匹配出现。


Data races

Some (or all) of the objects in the range [first,last) are accessed (once at most).
范围内一些元素将被访问。最少一个元素被访问。

Exceptions

Throws if any element comparison (or pred) throws or if any of the operations on iterators throws.

Note that invalid arguments cause undefined behavior.

如果任一元素比较或者迭代器操作抛出异常,则会抛出异常。

无效的参数导致未定义的行为。

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-1

于GDUT

——————————————————————————————————————————————————————————————————




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值