STL Algorithm 之 find

 find_if

template<class InputIterator, class Predicate>
   InputIterator find_if(
      InputIterator _First, 
      InputIterator _Last, 
      Predicate _Pred
   );

Parameters

_First

An input iterator addressing the position of the first element in the range to be searched.

_Last

An input iterator addressing the position one past the final element in the range to be searched.

_Pred

User-defined predicate function object that defines the condition to be satisfied by the element being searched for. A predicate takes single argument and returns true or false.

Return Value

An input iterator that addresses the first element in the range that satisfies the condition specified by the predicate.

find

template<class InputIterator, class Type>
   InputIterator find(
      InputIterator _First, 
      InputIterator _Last, 
      const Type& _Val
   );

Parameters

_First

An input iterator addressing the position of the first element in the range to be searched for the specified value.

_Last

An input iterator addressing the position one past the final element in the range to be searched for the specified value.

_Val

The value to be searched for.

Return Value

An input iterator addressing the first occurrence of the specified value in the range being searched. If no such value exists in the range, the iterator returned addresses the last position of the range, one past the final element.

find_first_of

template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2,
      BinaryPredicate _Comp
   );

Parameters

_First1

A forward iterator addressing the position of the first element in the range to be searched.

_Last1

A forward iterator addressing the position one past the final element in the range to be searched.

_First2

A forward iterator addressing the position of the first element in the range to be matched.

_Last2

A forward iterator addressing the position one past the final element in the range to be matched.

_Comp

User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied.

Return Value

A forward iterator addressing the position of the first element of the first subsequence that matches the specified sequence or that is equivalent in a sense specified by a binary predicate.

Remarks

The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.

The ranges referenced must be valid; all pointers must be dereferenceable and, within each sequence, the last position is reachable from the first by incrementation.

find_end

template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_end(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class Pr>
   ForwardIterator1 find_end(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2,
      BinaryPredicate _Comp
   );

Parameters

_First1

A forward iterator addressing the position of the first element in the range to be searched.

_Last1

A forward iterator addressing the position one past the final element in the range to be searched.

_First2

A forward iterator addressing the position of the first element in the range to be searched.

_Last2

A forward iterator addressing the position one past the final element in the range to be searched.

_Comp

User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied.

Return Value

A forward iterator addressing the position of the first element of the last subsequence that matches the specified sequence or that is equivalent in a sense specified by a binary predicate.

Remarks

The operator== used to determine the match between an element and the specified value must impose an equivalence relation between its operands.

The ranges referenced must be valid; all pointers must be dereferenceable and, within each sequence, the last position is reachable from the first by incrementation.

#include <iostream>
#include <vector>

using namespace std;

bool cmp(int& a)
{
	return a > 100;
}

bool match(int &a ,int &b)
{
	return a == b;
}

int main()
{
	vector<int> test;

	test.push_back(20);
	test.push_back(4);
	test.push_back(123);
	test.push_back(4);

	vector<int>match_table;
	match_table.push_back(1);
	match_table.push_back(2);
	match_table.push_back(3);
	match_table.push_back(4);

	vector<int>::iterator i;

	if ((i = find_if(test.begin(), test.end(), cmp)) != test.end())
		cout << (i - test.begin()) << "--->" << *i << endl;

	if ((i = find_if_not(test.begin(), test.end(), cmp)) != test.end())
		cout << (i - test.begin()) << "--->" << *i << endl;

	if ((i = find(test.begin(), test.end(), 4)) != test.end())
		cout << (i - test.begin()) << "--->" << *i << endl;

	if ((i = find_first_of(test.begin(), test.end(), match_table.begin(), match_table.end())) != test.end())
		cout << (i - test.begin()) << "--->" << *i << endl;

	if ((i = find_first_of(test.begin(), test.end(), match_table.begin(), match_table.end(), match)) != test.end())
		cout << (i - test.begin()) << "--->" << *i << endl;

	if ((i = find_end(test.begin(), test.end(), match_table.begin(), match_table.end(),match)) != test.end());
		cout << (i - test.begin()) << endl;

	getchar();


	return 0;
}

 

转载于:https://my.oschina.net/pirtt/blog/878052

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值