1.adjacent_find:
All containers are applicable (linear) adjacent_find (begin,end); Adjacent_find (begin,end,predicate);
Looking for the first occurrence of two consecutive equal elements between range [First,last], if present, returns to the first element iterator, otherwise returns last. You can also use your own defined two-dollar assertion (which is a custom-defined method)
/p>
#include <vector> #include <iostream> #include <stdio.h> #include <algorithm>using namespace std;/** A custom method that determines the equality of the condition is I=j*2*/bool myfunction (int i, int j) {return (i==j*2);} /**//All containers apply (linear) adjacent_find (b,e); Adjacent_find (B,E,P); Looking for the first occurrence of two consecutive equal elements between range [First,last], if present, returns to the first element iterator, otherwise returns last. You can also use your own defined method */int main () {int myints[] = {10,20,30,30,40,20,10,20}; The elements in the array are entered Myvector vector<int> myvector (myints,myints+8); Vector<int>::iterator it;//iterator printf ("initial element \ n"); For (It=myvector.begin (); It!=myvector.end (); ++it) cout<<*it<< ""; cout<<endl; Using the default comparison method it = Adjacent_find (Myvector.begin (), Myvector.end ()); if (It!=myvector.end ()) cout << "The first contiguous element is:" << *it << Endl; Using the custom comparison method it = Adjacent_find (Myvector.begin (), Myvector.end (), myfunction); if (It!=myvector.end ()) cout << "The second contiguous element is:" << *it << Endl; return 0;} /*****oUtput initial element 10 20 30 30 40 20 10 20 The first sequential element is: 30 The second continuous element is: 40*/
2.binary_search and includes:
Binary_search () //Two-point lookup, return bool value, Binary_search (Begin,end,value); Binary_search (begin,end,value,cmp); includes ( ) //contains a lookup that determines whether the container is a containment relationship and returns a bool value. Includes (Begin1,end1,begin2,end2); includes (BEGIN1,END1,BEGIN2,END2,CMP);
binary_search (): Returns True if any element exists within the range of [First,end] and Val is equal.
Otherwise, false is returned. Must be a sorted container
Includes (): two intervals must be sorted to determine whether the interval 1 contains the interval 2, the same range is [First,end]
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include < Algorithm>using namespace std;/**//all containers applicable (O (log (n))) ordered interval lookup algorithm binary_search ()//Two min lookup, return bool value, Binary_search (begin , end,value); Binary_search (begin,end,value,cmp); includes ()//contains lookup, determines whether the container is an inclusive relationship, returns a bool value. Includes (Begin1,end1,begin2,end2); includes (begin1,end1,begin2,end2,cmp); */bool myfunction (int i,int j) {return (i& LT;J);} int main () {int myints[] = {1,2,3,4,5,4,3,2,1}; Vector<int> V (myints,myints+9); 1 2 3 4 5 4 3 2 1//Use the default comparison sort (V.begin (), V.end ()); cout << "Find 3 ... "; if (Binary_search (V.begin (), V.end (), 3)) cout << "Discover!\n"; else cout << "not found. \ n"; Use a custom comparison sort (V.begin (), V.end (), myfunction); cout << "Find 6 ..."; if (Binary_search (V.begin (), V.end (), 6, MyFunction)) cout << "Discover!\n"; else cout << "not found. \ n"; cout<<endl;/**----------------------------includes () container contains lookup---------------------------**/int container[] = {5,15,10,25,20,35,30,50,45,40}; int continent[] = {40,30,20,10}; Sort (container,container+10); Sort (continent,continent+4); Use the default compare if (includes (container,container+10,continent,continent+4)) cout << "Container container contains container continent ! "<< Endl; Use a custom Comparison if (includes (Container,container+10,continent,continent+4, myfunction)) cout << "Container container Contains container continent! "<< Endl; return 0;} /**output Find 3 ... Found! Find 6 ... Not found. Container container contains container continent! Container container contains container continent!*/
3.count and Count_if:
Count: count (Begin,end,value); count_if: count_if (begin,end,predicate); equivalent member function of the associated container set.count Multiset.count map.count multimap.count
Count: This function uses a pair of iterators and a value to make a parameter that returns the statistical result of the number of occurrences of this value
COUNT_IF: Returns the number of elements in the interval that meet the specified criteria
#include <iostream> #include <vector> #include <set> #include <algorithm>using namespace std;/* * Odd */bool isodd (int i) {return i&1;} int main () {int mycount; Array count element int myints[] = {10,20,30,30,20,10,10,20}; 8 Elements mycount = (int) count (myints, myints+8, 10); cout << "10 appears" << Mycount << "times. \ n"; Create a new vector vector<int> myvector (myints, myints+8); Mycount = (int) count (Myvector.begin (), Myvector.end (), 20);//There are several cout << "20 appears" << Mycount << "Times. \ n"; /**************** Output:10 appeared 3 times. 20 appears 3 times. ///clear vector myvector.clear (); for (int i=1; i<10; i++) Myvector.push_back (i); Myvector:1 2 3 4 5 6 7 8 9 cout<< "\nmyvector:1 2 3 4 5 6 7 8 9 \ n"//mycount = (int) count_if (myvector. Begin (), Myvector.end (), isodd); Mycount = (int) count_if (Myvector.begin (), Myvector.end (), bind2nd (Modulus<int> (), 2));//= param1% 2 cout << "Myvector contains" << mycount << "odd element. \ n";//odd number of mating numbers not1,1 represents a parameter to take anti-mycount = (int) count_if (Myvector.begin (), Myvector.end (), Not1 (bind2nd (Modulus<int> (), 2))) ;//Express! (param1% 2) cout << "Myvector contains" << mycount << "even element. \ n";//Even/**************** Output: The myvector contains 5 odd elements. function Adapter Function object//bind2nd (Op,value); Indicates the second number of bindings param1 > 4 Here means the number of statistics greater than 4 mycount=count_if (Myvector.begin (), Myvector.end (), bind2nd (Greater<int> (), 4) ); cout<< "with" <<mycount<< "number greater than 4" <<endl;//expansion Exercise 4 &G T Param2 mycount=count_if (Myvector.begin (), Myvector.end (), bind1st (Greater<int> (), 4)); cout<< "with" <<mycount<< "number less than 4" <<endl;// 4 < ParaM2 mycount=count_if (Myvector.begin (), Myvector.end (), bind1st (Less<int> (), 4)); cout<< "with" <<mycount<< "number greater than 4" <<endl;// param1 >= 4 mycount=count_if (Myvector.begin (), Myvector.end (), bind2nd (Greater_equal<int> (), 4)); cout<< "with" <<mycount<< "number greater than or equal to 4" <<endl;// param1 <= 4 mycount=count_if (Myvector.begin (), Myvector.end (), bind2nd (Less_equal<int> (), 4)); cout<< "with" <<mycount<< "number less than equals 4" <<endl;/**** associated container ****/multiset<int> MS (Myvector.begin (), Myvector.end ()); Ms.insert (Myvector.begin (), Myvector.begin () +6); Ms.insert (Myvector.begin (), Myvector.begin () +4); Ms.insert (Myvector.begin (), Myvector.begin () +2); Ms.insert (1); Multiset<int>::iterator Ims=ms.begin (); while (Ims!=ms.end ()) {cout<<*ims++<< ""; }cout<<endl;//two ways to find 1Number int Cnt=count (Ms.begin (), Ms.end (), 1);//all containers are applicable but slower cout<< "Multiset" <<cnt<< "1". <<endl; Cnt=ms.count (1);//associated container exclusive set already sorted can be quickly counted cout<< "Multiset" <<cnt<< "1". <<endl; return 0;}