//----------------------------------------------------------------------------------------
// 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;
}
// 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;
}