dimanche 28 février 2016

for_each function in c++11

I have vector of integers filled with 5 numbers and i'm curious what exactly does [&idx] do in :

int idx = 0;
for_each ( x . begin (), x . end (), [&idx] ( const int & a ) { cout << idx ++ << " " << a << endl; } );`

Why it does not work like this ? :

int idx = 0;
  for_each ( x . begin (), x . end (), ( const int & a, int & idx ) { cout << idx ++ << " " << a << endl; } );

Is it better from perfomance point of view than? :

for ( vector<int>::size_type i = 0; i < x . size (); i ++ )
    cout << x[i] << endl;

Aucun commentaire:

Enregistrer un commentaire