cpp20 features

With advent to range-based for loop we begin to write the below loop

for( int i=0; i < vec.size(); ++i){ //vec is just for sample
  // use vec[i]
}

as this one.

for( auto v: vec){
  // use v
}

However at many time we also needed the index, we have do this dirty work.

int index =0;
for( auto v: vec){
  // use v
  ++index;
}

But, we do not have to play on dirt now using c++20 we can declare variable inside the range for loop. :)

for(int index{0}; auto v: vec){
  // use v
  ++index;
}

Refs

★ 2 min read · Rajesh Pandian M · c , cpp , tricks