您的位置:首页 > 编程语言 > Go语言

STL Algorithm 之 find

2017-04-13 00:00 169 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL Algorithm find