STL generic Algorithm
# Include <iostream> # include <vector> # include <algorithm> # include <iterator> # include <numeric> # include <list> Using STD: cout; Using STD :: endl; Using STD: vector; Using STD: List; bool isoushu (const Int & nnum); bool isbigger (const Int & nfirst, const Int & nsecond ); int main () {vector <int> ivec; For (INT I = 0; I <10; ++ I) ivec. push_back (I); cout <Endl; typedef vector <int> ivec; // STD: Find ivec: const_it Erator iter = STD: Find (ivec. Begin (), ivec. End (), 5); If (ivec. End ()! = ITER) cout <Endl <"the value is" <* ITER <Endl; else cout <Endl <"can not find the value" <5 <Endl; // STD: Accumulate int nsum = STD: Accumulate (ivec. begin (), ivec. end (), 100); cout <Endl <"the sum is" <nsum <Endl; cout <Endl; // fill vector <int> ivec2 (20); STD: Fill (ivec2.begin (), ivec2.end (), 100); For (ivec: const_iterator iter = ivec2.begin (); ITER! = Ivec2.end (); ++ ITER) cout <* ITER <","; cout <Endl; // fill_n vector <int> ivec3 (5); STD :: fill_n (back_inserter (ivec3), 10,100); cout <Endl <"size of ivec3 is" <ivec3.size () <Endl; For (ivec :: const_iterator iter = ivec3.begin (); iter! = Ivec3.end (); ++ ITER) cout <* ITER <","; cout <Endl; // copy vector <int> ivec4; list <int> lst1; For (INT I = 0; I <10; ++ I) lst1.push _ back (I); STD: Copy (lst1.begin (), lst1.end (), back_inserter (ivec4); For (ivec: const_iterator iter = ivec4.begin (); iter! = Ivec4.end (); ++ ITER) cout <* ITER <","; cout <Endl; // copy vector <int> ivec5 (11 ); STD: Copy (lst1.begin (), lst1.end (), ivec5.begin (); For (ivec: const_iterator iter = ivec5.begin (); iter! = Ivec5.end (); ++ ITER) cout <* ITER <","; cout <Endl; // replace list <int> lst2; for (INT I = 0; I <10; ++ I) lst2.push _ back (I * 2); cout <Endl; // print the cout value before replace <Endl <"print the value before lst2 Replace" <Endl; For (list <int>: const_iterator iter = lst2.begin (); ITER! = Lst2.end (); ++ ITER) cout <* ITER <","; cout <Endl; cout <"Print replace to value" <Endl; STD: Replace (lst2.begin (), lst2.end (), 8,888); For (list <int >:: const_iterator iter = lst2.begin (); iter! = Lst2.end (); ++ ITER) cout <* ITER <","; cout <Endl; // replace_copy list <int> lst3 (lst2.size (); STD: replace_copy (lst2.begin (), lst2.end (), lst3.begin (), 888,999 ); cout <Endl <"Print lst2 replace_copy after lst3 to value" <Endl; For (list <int>: const_iterator iter = lst3.begin (); iter! = Lst3.end (); ++ ITER) cout <* ITER <","; cout <Endl; // stable_sort vector <int> ivec6; for (INT I = 0; I <10; ++ I) ivec6.push _ back (I); cout <Endl; cout <Endl <"print before stable_sort to ivec6 to value" <Endl; For (ivec: const_iterator iter = ivec6.begin (); iter! = Ivec6.end (); ++ ITER) cout <* ITER <","; cout <Endl <"Print stable_sort to ivec6 to value" <Endl; STD: stable_sort (ivec6.begin (), ivec6.end (), isbigger); For (ivec: const_iterator iter = ivec6.begin (); iter! = Ivec6.end (); ++ ITER) cout <* ITER <","; cout <Endl; // count_if cout <Endl <"calculate the number of even numbers in ivec6" <Endl; int nnums = STD: count_if (ivec6.begin (), ivec6.end (), isoushu ); cout <Endl <"ivec6 contains" <nnums <"<Endl; cout <Endl; cout <"\ nthis is main function \ n"; return 0;} // stable_sort sort bool isbigger in descending order (const Int & nfirst, const Int & nsecond) {return nfirst> nsecond;} // It is an even bool isoushu (const Int & nnum) {return (0 = nnum % 2 );}
Execution result