#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
//STL:线性查找
int main()
{
int a[] = {4, 1, 6, 9, 7, 1, 0};
size_t len = sizeof a / sizeof a[0];
typedef std::vector<int> vctype_int;
vctype_int vi(a, a + len);
/************************************************************************/
//find
//find_if
/************************************************************************/
//find:寻找范围内某一特定值,返回满足条件的第一个元素的迭代器
/*
template<class InputIterator, class Type>
InputIterator find(
InputIterator _First,
InputIterator _Last,
const Type& _Val
);
*/
vctype_int::iterator it = std::find(vi.begin(),vi.end(), 2);
//it = std::find(begin(a), end(a), 2);
if(it == vi.end())
std::cout << "not found" << std::endl;//not found
else
std::cout << "found" << std::endl;
//find_if:寻找范围内满足条件的第一个元素的迭代器,一般为不等于某个值的条件
//当然也可以使用find_if来查找某个特定的值
//如上代码可以改成如下代码:
it = find_if(vi.begin(), vi.end(), bind1st(std::equal_to<int>(),2));
if(it == vi.end())
std::cout << "not found" << std::endl;//not found
else
std::cout << "found" << std::endl;
/*
template<class InputIterator, class Predicate>
InputIterator find_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
*/
/*
template<class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type count_if(
InputIterator _First,
InputIterator _Last,
Predicate _Pred
);
*/
int val = 8;
it = std::find_if(vi.begin(), vi.end(), [val](const int& e){return e > val;});
if(it == vi.end())
std::cout << "no elements is greater than" << val << std::endl;
else
{
int nums = std::count_if(vi.begin(), vi.end(), [val](const int& e){return e > val;});
std::cout << "has " << nums << " elements greater than " << val << std::endl;
//has 1 elements greater than 8
}
//绑定操作(比较)函数的第一个参数
/*
template<class Operation, class Type>
binder1st <Operation> bind1st(
const Operation& _Func,
const Type& _Left
);
*/
find_if(vi.begin(), vi.end(), bind1st(std::less<int>(),8));
if(it == vi.end())
std::cout << "no elements is greater than" << val << std::endl;
else
{
int nums = std::count_if(vi.begin(), vi.end(), bind1st(std::less<int>(),8));
std::cout << "has " << nums << " elements greater than " << val << std::endl;
//has 1 elements greater than 8
}
//绑定操作(比较)函数的第二个参数
/*
template<class Operation, class Type>
binder2nd <Operation> bind2nd(
const Operation& _Func,
const Type& _Right
);
*/
find_if(vi.begin(), vi.end(), bind2nd(std::less<int>(),8));
if(it == vi.end())
std::cout << "no elements is greater than" << val << std::endl;
else
{
int nums = std::count_if(vi.begin(), vi.end(), bind2nd(std::less<int>(),8));
std::cout << "has " << nums << " elements less than " << val << std::endl;
//has 6 elements less than 8
}
/************************************************************************/
//adjacent_find
/************************************************************************/
//版本1:查找两个相等的相邻元素,operator==
//版本2:查找两个相邻元素是否满足某种条件,如果满足条件,则返回第一个满足条件的,function compare
/*
template<class ForwardIterator>
ForwardIterator adjacent_find(
ForwardIterator _First,
ForwardIterator _Last
);
*/
/*
template<class ForwardIterator , class BinaryPredicate>
ForwardIterator adjacent_find(
ForwardIterator _First,
ForwardIterator _Last,
BinaryPredicate _Comp
);
*/
//测试版本1:
int b[] = {1, 2, 6, 4, 4, 6, 7};
len = sizeof b / sizeof b[0];
int *p = std::adjacent_find(std::begin(b), std::end(b));
if(p != b + len)
std::cout << "Duplicate element: " << *p << std::endl;//Duplicate element: 4
vi.clear();
vi.assign(b, b + len);
it = std::adjacent_find(vi.begin(), vi.end());
if(it != vi.end())
std::cout << "Duplicate element: " << *it << std::endl;//Duplicate element: 4
//测试版本2:
p = std::adjacent_find(std::begin(b), std::end(b), std::greater<int>());
if(p == b + len)//未找到greater条件的元素,即升序
std::cout << "Elements are sorted in ascending order! " << std::endl;
else
{
//Element 2 is out of order:6 > 4.即查找到greater条件的元素对
std::cout << "Element " << p - b << " is out of order:" << *p << " > " << *(p + 1) << "." << std::endl;
}
/************************************************************************/
//find_first_of
/************************************************************************/
//find_first_of:在范围[first1,last1)内查找任何出现与[first2,last2)内的元素,
//类似strpbrk
/*
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
);
*/
const char *WS = " \t\n";
const int n_WS = strlen(WS);
char *s1 = "There sentence contains five words.";
char *s2 = "OneWord";
char *end1 = std::find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);
char *end2 = std::find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);
//first word of s1 : 5.
std::cout << "first word of s1 : " << end1 - s1 << "." << std::endl;
//first word of s1 : 7.
std::cout << "first word of s1 : " << end2 - s2 << "." << std::endl;
return 0;
}
运行结果:
not found
not found
has 1 elements greater than 8
has 1 elements greater than 8
has 6 elements less than 8
Duplicate element: 4
Duplicate element: 4
Element 2 is out of order:6 > 4.
first word of s1 : 5.
first word of s1 : 7.
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | ![]() |
![]() |