C++STL之简单查找算法

//----------------------------------------------------------------------------------------  
//      Desc:       STL_find() used in struct data  
//      Author:     spring brother  
//      Data:       2018.3.24  
//      Copyright (C) 2018 spring brother  
//----------------------------------------------------------------------------------------  

/*
简单查找算法
find(beg,end,val)
find_if(beg,end,unaryPred)
find_if_not(beg,end,unaryPred)
count(beg,end,val)
count_if(beg,end,unaryPred)

find返回一个迭代器,指向输入序列中第一个等于val的元素。

find_if返回一个迭代器,指向第一个满足unaryPred的元素。

find_if_not返回一个迭代器,指向第一个不满足unaryPred的元素。上述三个算法在未找到元素时都返回end。

count返回一个计数器,指出val出现过多少次;count_if统计有多少个元素满足unaryPred。

all_of(beg,end,unaryPred)
aay_of(beg,end,unaryPred)
none_of(beg,end,unaryPred)

这些算法都返回一个bool值,分别指出unaryPred是否对所有的元素都成功、对任意元素成功以及对所有元素
都不成功。如果序列为空,any_of返回false,而all_of和none_of返回true。
*/
#include "stdafx.h"
#include <algorithm>
#include <iostream>

using namespace std;

bool isEqual(int &i) {
return i == 3;
}
bool isGreater(int &i) {
return i > 0;
}
int main()
{
int arrIn[5] = { 1,2,3,3,5 };
int val = 2;
int *temp = find(arrIn, arrIn + 5, val);  //find
if (temp != arrIn + 5) {
cout << "find = " << *temp << endl;
}
temp = find_if(arrIn, arrIn + 5, isEqual);  //find_if,传入仿函数
if (temp != arrIn + 5) {
cout << "find_if = " << *temp << endl;
}
temp = find_if(arrIn, arrIn + 5, [](int &i) {return i == 5; });  //find_if,传入匿名函数
if (temp != arrIn + 5) {
cout << "find_if = " << *temp << endl;
}

temp = find_if_not(arrIn, arrIn + 5, isEqual);  //find_if_not,传入仿函数
if (temp != arrIn + 5) {
cout << "find_if_not = " << *temp << endl;
}
temp = find_if_not(arrIn, arrIn + 5, [](int &i) {return i == 5; });  //find_if_not,传入匿名函数
if (temp != arrIn + 5) {
cout << "find_if_not = " << *temp << endl;
}

int val_1 = 3;
int num = count(arrIn, arrIn + 5, val_1);  //count
if (temp != arrIn + 5) {
cout << "count = " << num << endl;
}
num = count_if(arrIn, arrIn + 5, isEqual);  //count_if,传入仿函数
if (temp != arrIn + 5) {
cout << "count_if = " << num << endl;
}
num = count_if(arrIn, arrIn + 5, [](int &i) {return i == 5; });  //count_if,传入匿名函数
if (temp != arrIn + 5) {
cout << "count_if = " << num << endl;
}

int b = all_of(arrIn, arrIn + 5, isGreater);  //all_of,传入仿函数
if (b) {
cout << "all_of is true!\n";
}
else {
cout << "all_of is false!\n";
}
b = all_of(arrIn, arrIn + 5, [](int &i) {return i > 2; });  //all_of,传入匿名函数
if (b) {
cout << "all_of is true!\n";
}
else {
cout << "all_of is false!\n";
}

b = any_of(arrIn, arrIn + 5, isGreater);  //any_of,传入仿函数
if (b) {
cout << "any_of is true!\n";
}
else {
cout << "any_of is false!\n";
}
b = any_of(arrIn, arrIn + 5, [](int &i) {return i > 4; });  //all_of,传入匿名函数
if (b) {
cout << "any_of is true!\n";
}
else {
cout << "any_of is false!\n";
}

b = none_of(arrIn, arrIn + 5, isGreater);  //none_of,传入仿函数
if (b) {
cout << "none_of is true!\n";
}
else {
cout << "none_of is false!\n";
}
b = none_of(arrIn, arrIn + 5, [](int &i) {return i > 5; });  //none_of,传入匿名函数
if (b) {
cout << "none_of is true!\n";
}
else {
cout << "none_of is false!\n";
}

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值