STL often uses traversal algorithms for_each and transform.

Source: Internet
Author: User

For_each () and transform () algorithms compare
1) STL Algorithm – Modified algorithm
For_each ()
Copy ()
Copy_backward ()
Transform ()
Merge ()
Swap_ranges ()
Fill ()
Fill_n ()
Generate ()
Generate_n ()
Replace
Replace_if ()
Replace_copy ()
Replace_copy_if ()
2)
For_each () speed is not flexible
Transform () Slow and flexible


Normal: The function object used by For_each, which is a reference, no return value
The function object used by the transform, which generally does not use references, but also return values

int showElem2 (int n) {cout << n << ""; return n;} void Main43_transform_pk_foreach () {vector<int> v1;v1.push_back (1); V1.push_back (3); V1.push_back (5); vector <int> v2 = V1;for_each (V1.begin (), V1.end (), Showelem),//transform Requirements for function objects/*c:\program Files\Microsoft Visual Studio 10.0\vc\include\algorithm (1119): See Instantiating a function template that is being compiled "_outit std::_transform1<int*,_outit,void (__cdecl *) (int &) > (_init,_init,_outit,_fn1,std::tr1::true_type) reference 1> with1> [1> _outit=st              D::_vector_iterator<std::_vector_val<int,std::allocator<int>>>,1> _InIt=int *,1> _fn1=void (__cdecl *) (int &) 1>]*//*template<class _init,class _outit,class _fn1> Inline_ou  Tit _transform (_init _first, _init _last,_outit _dest, _fn1 _func) {//Transform [_first, _last) with _funcfor (; _first! = _last;  ++_first, ++_dest) *_dest = _func (*_first); Explains why there is a return value of returns (_dest);} */transform (V2.begin (),V2.end (), V2.begin (), showElem2);} 



The demo demonstrates the use of for_each and transform algorithms in a complete way. And

#include <iostream> #include <cstdio> #include <vector> #include <list> #include <algorithm > #include <functional> #include <iterator>using namespace Std;class cmyshow{public:cmyshow () {num = 0;} void operator () (int &n) {num++;cout << n << "";} void Printnum () {cout << "num:" << num << Endl;} Protected:private:int num;};  void Printv (vector<int> &v) {for (Vector<int>::iterator it = V.begin (); It! = V.end (); it++) {cout << *it << "";} cout << Endl;} void Printlist (list<int> &v) {for (List<int>::iterator it = V.begin (); It! = V.end (); it++) {cout << * It << "";} cout << Endl;} void Showelem (int &n) {cout << n << "";} For_each function Use method void Play_for_each () {vector<int> v1;v1.push_back (1); V1.push_back (3); V1.push_back (5);p Rintv (v1); cout << endl;/*template<class _init,class _fn1> inline_fn1 for_each (_init _first, _init _Last, _Fn1 _Fun c) {//Perform function for each element_debug_range (_first, _last); _debug_pointer (_func); return (_for_each (_unchecked (_ First), _unchecked (_last), _func);} *///Function Object callback function entry address For_each (V1.begin (), V1.end (), Showelem), cout << Endl;for_each (V1.begin (), V1.end (), Cmyshow () ); cout << Endl; Cmyshow Mya; Cmyshow my1 = For_each (V1.begin (), V1.end (), MYA);  Initialize Mya.printnum () to my1;  MA1 and My1 are two different objects my1.printnum (); my1 = For_each (V1.begin (), V1.end (), MYA); Assign a value to My1 my1.printnum ();} int increase (int i) {return i + 100;} Transform function Use method void Play_transform () {vector<int> v1;v1.push_back (1); V1.push_back (3); V1.push_back (5); Printv (v1); cout << Endl;//transform Use callback Functions transform (V1.begin (), V1.end (), V1.begin (), increase);p Rintv (v1); cout << endl;//transform Use a predefined function object transform (V1.begin (), V1.end (), V1.begin (), negate<int> ());p Rintv (v1 ); cout << endl;//transform use function adapters and function Objects list<int> mylist;mylist.resize (V1.size ()), transform (V1.begin (), V1.end (), Mylist.begin (), bind2nd (Multiplies<int> ()));p rintlist (mylist), cout << Endl;//transform can also output the results of the operation directly to the screen transform ( V1.begin (), V1.end (), ostream_iterator<int> (cout, ""), negate<int> ()); cout << Endl;} Under normal circumstances: The function object used by the For_each, the reference is the parameter. There is no function object for the return value//transform, the reference is generally not used, but there is a return value of int showElem2 (int n) {cout << n << ""; return n;} void Play_transform_pk_foreach () {vector<int> v1;v1.push_back (1); V1.push_back (3); V1.push_back (5);vector< Int> v2 = V1;for_each (V1.begin (), V1.end (), Showelem),//transform Requirements for function objects/*c:\program Files\Microsoft Visual Studio 10.0\vc\include\algorithm (1119): See the instantiation of a function template being compiled "_outit std::_transform1<int*,_outit,void (__cdecl *) (int &) > (_init,_init,_outit,_fn1,std::tr1::true_type) "Reference 1> with1> [1> _outit=std::_ve              Ctor_iterator<std::_vector_val<int,std::allocator<int>>>,1> _InIt=int *,1> _fn1=void (__cdecl *) (int &) 1>]*//*template<class _InIt,class _OutIt,class _Fn1> inline_OutIt _Transform(_InIt _First, _InIt _Last,_OutIt _Dest, _Fn1 _Func  ) {//Transform [_first, _last) with _funcfor (; _first! = _last; ++_first, ++_dest) *_dest = _func (*_first); Explains why there is a return value of returns (_dest);} */transform (V2.begin (), V2.end (), V2.begin (), showElem2);} int main () {Play_for_each ();p lay_transform ();p Lay_transform_pk_foreach (); return 0;}

STL often uses traversal algorithms for_each and transform.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.