The following content below has been taken from https://www.unrealengine.com/en-US/blog/ranged-based-for-loops?sessionInvalidated=true
…Here is an example of how this can really clean up common game code. Previously if I wanted to iterate over an array of AActor* pointers, it used to look like:
TArray<AActor*> Actors; for (int32 ActorIndex=0; ActorIndex<Actors.Num(); ActorsIndex++) { AActor* Actor = Actors[ActorIndex]; Actor->SetActorLocation(NewLocation); }
But using the ‘range-based for loop’ syntax it now looks like:
TArray<AActor*> Actors; for (AActor* Actor : Actors) { Actor->SetActorLocation(NewLocation); }
How nice is that! Here is another example with a TMap – it used to look like this:
TMap<AActor*, FMyStruct> ActorStructs; for (auto ActorStructIt = ActorStructs.CreateIterator(); ActorStructIt; ++ActorStructIt) { AActor* Actor = ActorStructIt.Key(); FMyStruct& Data = ActorStructIt.Value(); }
But now it looks like:
TMap<AActor*, FMyStruct> ActorStructs; for (const auto& Entry : ActorStructs) { AActor* Actor = Entry.Key; const FMyStruct& Data = Entry.Value; }