#include<iostream>#include<vector>#include<algorithm>//all_oftemplate<typenameT>classTest_std_algorithm{public:voidadd_vector_para(const T& a){
vec.push_back(a);}//none_of. Return true if prefunction returns false for all elements in the range[first, end) or if range is empty, and false otherwise.voidtest_none_of(){if(std::none_of(vec.begin(), vec.end(),[](const T& i){return i <0;})){
std::cout <<"true"<< std::endl;}else{
std::cout <<"false"<< std::endl;}}//for_each. Applies function to each of the elements in the range [first, last).voidtest_for_each(){
std::for_each(vec.begin(), vec.end(),[](const T& i){std::cout << i << std::endl;});}voidtest_find(const T& val){
it = std::find(vec.begin(), vec.end(), val);if(vec.end()!= it){
std::cout <<"found at the position"<<": "<<(it - vec.begin())<< std::endl;}else{
std::cout <<"No element"<< std::endl;}}//find_if. Returns an iterator to the first element in the range [first, last) for which predfunction return true.//if no_elements is found, the function returns last.voidtest_find_if(bool(*func)(const T& i)){
it = std::find_if(vec.begin(), vec.end(),(*func));if(vec.end()!= it){
std::cout <<"found at the position"<<": "<<(it - vec.begin())<< std::endl;}else{
std::cout <<"No element"<< std::endl;}}//find_if_not. Returns an iterator to the first element in the range [first, last) for which predfunction return false.//if no_elements is found, the function returns last.voidtest_find_if_not(bool(*func)(const T& i)){
it = std::find_if_not(vec.begin(), vec.end(),(*func));if(vec.end()!= it){
std::cout <<"found at the position"<<": "<<(it - vec.begin())<< std::endl;}else{
std::cout <<"No element"<< std::endl;}}//在指定搜索范围查找指定序列,返回找到的第一个元素的迭代器voidtest_find_end(){
T needle[]={2,3,4};
it = std::find_end(vec.begin(), vec.end(), needle, needle +3);if(it != vec.end()){
std::cout <<"found at the position "<<(it - vec.begin())<< std::endl;}else{
std::cout <<"No element"<< std::endl;}}voidtest_find_end(bool(*func)(const T& i,const T& j)){
T needle[]={4,5,6};
it = std::find_end(vec.begin(), vec.end(), needle, needle +3,(*func));if(it != vec.end()){
std::cout <<"found at the position "<<(it - vec.begin())<< std::endl;}else{
std::cout <<"No element"<< std::endl;}}//返回匹配到的第一个元素的迭代器voidtest_find_first_of(){
T needle[]={2,3,4};
it = std::find_first_of(vec.begin(), vec.end(), needle, needle +3);if(it != vec.end()){
std::cout <<"The first match is: "<<*it << std::endl;}else{
std::cout <<"No element"<< std::endl;}}voidtest_find_first_of(bool(*func)(const T& i,const T& j)){
T needle[]={4,5,6};
it = std::find_first_of(vec.begin(), vec.end(), needle, needle +3,(*func));if(it != vec.end()){
std::cout <<"The first match is: "<<*it << std::endl;}else{
std::cout <<"No element"<< std::endl;}}//Searches the range [first,last) for the first occurrence of two consecutive elements that match,//and returns an iterator to the first of these two elements, or last if no such pair is found.voidtest_adjacent_find(){
it = std::adjacent_find(vec.begin(), vec.end());if(it != vec.end()){
std::cout <<"The first match is: "<<*it << std::endl;}else{
std::cout <<"No element"<< std::endl;}}voidtest_adjacent_find(bool(*func)(const T& i,const T& j)){
it = std::adjacent_find(vec.begin(), vec.end());if(it != vec.end()){
std::cout <<"The first match is: "<<*it << std::endl;}else{
std::cout <<"No element"<< std::endl;}}//Returns the number of elements in the range [first,last) that compare equal to val.voidtest_count(const T& val){
std::cout <<"The number of "<< val <<" is "<< std::count(vec.begin(), vec.end(), val)<< std::endl;}voidtest_count(const T& val,bool(*func)(const T& i,const T& j)){
std::cout <<"The number of "<< val <<" is "<< std::count(vec.begin(), vec.end(), val)<< std::endl;}//Reuturn the number of elements in the range [first, last) for whitch pred is true.voidtest_conut_of(bool(*func)(const T& i)){
std::cout <<"The number of True is "<< std::count_if(vec.begin(), vec.end(),(*func))<< std::endl;}//将范围[first1,last1) 中的元素与从first2开始的范围中的元素进行比较,并返回两个序列中不匹配的第一个元素。//该函数返回一个一对每个范围中不匹配的第一个元素的迭代器。一种 一对, 其成员 第一的 和 第二指向两个序列中比较不相等的第一个元素。voidtest_mismatch(){
T ints[]={1,2,11};
std::pair<std::vector<T>::iterator, T*> mypair;
mypair = std::mismatch(vec.begin(), vec.end(), ints);
std::cout <<"First mismatching elements: "<<*mypair.first;
std::cout <<" and "<<*mypair.second << std::endl;}voidtest_mismatch(bool(*func)(const T& i,const T& j)){
T ints[]={1,2,11};
std::pair<std::vector<T>::iterator, T*> mypair;
mypair = std::mismatch(vec.begin(), vec.end(), ints,(*func));
std::cout <<"First mismatching elements: "<<*mypair.first;
std::cout <<" and "<<*mypair.second << std::endl;}//将范围内的元素[first1,last1)与从first2开始的范围内的元素进行比较,如果两个范围内的所有元素都匹配,则返回true。voidtest_equal(){
T ints[]={1,2,3,4,5,6,7,8,9,10};if(std::equal(vec.begin(), vec.end(), ints))
std::cout <<"The contents of both sequences are equal.\n";else
std::cout <<"The contents of both sequences differ.\n";}voidtest_equal(bool(*func)(const T& i,const T& j)){
T ints[]={1,2,3};if(std::equal(vec.begin(), vec.end(), ints,(*func)))
std::cout <<"The contents of both sequences are equal.\n";else
std::cout <<"The contents of both sequences differ.\n";}private:
std::vector<T> vec ={1,2,3,4,5,6,7,8,9,10};typenamestd::vector<T>::iterator it;};intmain(){
Test_std_algorithm<int> test_std;//test_std.test_find(5);//test_std.test_find_if([](const int& i)->bool {return i > 5; });//test_std.test_find_if_not([](const int& i)->bool {return i > 5; });//test_std.test_find_end();//test_std.test_find_end([](const int& i, const int& j)->bool {return i == j; });//test_std.test_find_first_of();//test_std.test_find_first_of([](const int& i, const int& j)->bool {return i == j; });//test_std.add_vector_para(10);//test_std.test_adjacent_find();//test_std.test_adjacent_find([](const int& i, const int& j)->bool {return i == j; });//test_std.test_count(10);//test_std.test_count(10, [](const int& i, const int& j)->bool {return i == j; });//test_std.test_conut_of([](const int& i)->bool {return (i > 5); });//test_std.test_mismatch();//test_std.test_mismatch([](const int& i, const int& j)->bool {return i == j; });//test_std.test_equal();
test_std.test_equal([](constint& i,constint& j)->bool{return i == j;});system("pause");return0;}