/* adjacent_find 查找两个相邻的等价元素*/
#include#include #include using namespace std; /* adjacent_find 查找两个相邻的等价元素*/ bool myfunction(int i , int j) { return (i == j); } void adjacentFineFunction() { int myints[] = {5,5,51,30,30,20,10,10,20}; vector myvector (myints,myints+8); vector ::iterator it; //using default comparison it = adjacent_find(myvector.begin(), myvector.end()); if (it != myvector.end()) cout<<"the first pair of repeated elements are : "<<*it <
输出结果the first pair of repeated elements are : 5 the second pair of repeated elements are: 30
/*all_of 检测在给定范围中是否所有元素都满足给定的条件*/
#include#include #include using namespace std; /*all_of 检测在给定范围中是否所有元素都满足给定的条件*/ int main(int argc, const char * argv[]) { array foo = {3,5,7,11,13,17,19,13}; if (all_of(foo.begin(), foo.end(), [](int i){ return i % 2;})) { cout <<"All the elements are odd numbers"<
输出结果All the elements are odd numbers
/*any_of 检测在给定范围中是否存在元素满足给定条件*/
#include#include #include using namespace std; /*any_of 检测在给定范围中是否存在元素满足给定条件*/ int main(int argc, const char * argv[]) { array foo = {0,1,-1,3,-3,5,-5}; if (any_of(foo.begin(), foo.end(), [](int i){return i< 0;})) { cout<<"There are negative elements in the range."<
输出结果:There are negative elements in the range.
/*count 返回值等价于给定值的元素的个数*/
#include#include #include using namespace std; /*count 返回值等价于给定值的元素的个数*/ int main(int argc, const char * argv[]) { int myints[] = {10,20,30,30,20,10,10,20}; //8 elements int mycount = (int)count(myints, myints + 8, 10); cout<< "10 appears " < myvector(myints,myints + 8); mycount = (int)count(myvector.begin(), myvector.end(), 20); cout<< "20 appears " << mycount <<" times."<
输出结果:10 appears 3 times. 20 appears 3 times.
/*count_if 返回值满足给定条件的元素的个数*/
/*count_if 返回值满足给定条件的元素的个数*/ #include//std::count #include //std::count_if #include //std::vector using namespace std; bool IsOdd(int i){return ((i % 2) == 1);} int main(int argc, const char * argv[]) { vector myvector; for (int i = 1; i < 10; i++) { myvector.push_back(i); // myvector : 1 2 3 4 5 6 7 8 9 } int mycount = (int)count_if(myvector.begin(), myvector.end(), IsOdd); cout<<"myvector contains "<
输出结果:myvector contains 5odd values.
/*equal 返回两个范围是否相等*/
/*equal 返回两个范围是否相等*/ #include#include #include using namespace std; bool mypredicate(int i, int j) { return (i == j); } int main(int argc, const char * argv[]) { int myints[] = {20,40,60,80,100}; vector myvector(myints , myints + 5); //using default comparison: if (equal(myvector.begin(), myvector.end(), myints)) { cout <<"The contents of both sequences are equal."<
输出结果:The contents of both sequences are equal.
/*find 返回第一个值等价于给定值得元素*/
#include#include #include using namespace std; int main(int argc, const char * argv[]) { int myints[] ={ 10, 20, 30 ,40 }; int* p; p = find(myints, myints + 4, 30); ++p; cout<<"The element following 30 is " <<*p< myvector(myints,myints + 4); vector ::iterator it; it = find(myvector.begin(), myvector.end(), 30); ++it; cout<<"The element following 30 is " << *it <
输出结果:
The element following 30 is 40 The element following 30 is 40
/*find_end 查找范围 A 中与范围 B等价的子范围最后出现的位置*/
#include#include #include using namespace std; bool myfunction(int i,int j) { return (i == j); } int main(int argc, const char * argv[]) { int myints[] = {1,2,3,4,5,1,2,3,4,5}; vector haystack(myints,myints + 10); int needle1[] = {2,3,4}; //using default comparison: vector ::iterator it; it = find_end(haystack.begin(), haystack.end(), needle1, needle1 + 3); if (it != haystack.end()) cout<< "needle1 last found at position " <<(it - haystack.begin())<
结构输出:needle1 last found at position 6 needle2 last found at position 4
/*find_first_of 查找范围 A 中第一个与范围 B 中任一元素等价的元素的位置*/
#include#include #include #include using namespace std; bool comp_case_insensitive(char c1,char c2){ return (tolower(c1) == tolower(c2)); } int main(int argc, const char * argv[]