# TArray iteration

**URL:** <https://forums.unrealengine.com/t/tarray-iteration/339912>\
**Category:** C++\
**Tags:** tarray, for-loop, unreal-engine, UE4, question, CPP\
**Created:** [November 28, 2015, 4:31am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912 "2015-11-28T04:31:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirt113](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dirt113](https://forums.unrealengine.com/u/Dirt113)\
**Post date:** [November 28, 2015, 4:31am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/1 "2015-11-28T04:31:44Z")

</div>

Is this iteration system correct?

Or is there a more efficient way to do the search?

```
/** @returns list of most important checkpoints of overlapped
 *
 * @param IgnoredCheckpoints Checkpoints to exclude from search
 */
TArray<ADHCheckpoint*> ADHPawn::GetImportantCheckpoints(TArray<ADHCheckpoint*> IgnoredCheckpoitns) const
{
	TArray<ADHCheckpoint*> ReturnValue;
	int32 CurrentRecord = -1;

	int32 Counter, ArrayLength;
	for (Counter = 0, ArrayLength = OverlappedCheckpoints.Num(); Counter <= ArrayLength; Counter += 1)
	{
		bool bSkipIteration = false;

		int32 IgnoreCounter, IgnoreLength;
		for (IgnoreCounter = 0, IgnoreLength = IgnoredCheckpoitns.Num(); IgnoreCounter <= IgnoreLength; IgnoreCounter += 1)
		{
			bSkipIteration = bSkipIteration || (OverlappedCheckpoints[Counter] == IgnoredCheckpoitns[IgnoreCounter]);
		}

		if (!bSkipIteration && (OverlappedCheckpoints[Counter]->CheckpointImportance >= CurrentRecord))
		{
			CurrentRecord = OverlappedCheckpoints[Counter]->CheckpointImportance;
			ReturnValue.AddUnique(OverlappedCheckpoints[Counter]);
		}
	}

	// Re-using iteration counters here is totally fine
	for (Counter = 0, ArrayLength = ReturnValue.Num(); Counter <= ArrayLength; Counter += 1)
	{
		if (ReturnValue[Counter]->CheckpointImportance < CurrentRecord) ReturnValue.Remove(ReturnValue[Counter]);
	}

	return ReturnValue;
}
```

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [November 28, 2015, 4:54am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/2 "2015-11-28T04:54:39Z")

</div>

It should be like this, remeber that size or array is bigger by 1 then maximum index, sho you should not use \<=, you also as you can see you dont need to declere varabels needed in loop

```
int32 Size = ReturnValue.Num();
for(int32 Counter = 0; Counter < Size; Counter++)

```

But there a lot nicer way in C++11 (which newest compilers supports and UE4 actully require it) it’s like foreach 🙂

```
for(ADHCheckpoint* Check : ReturnValue) {
    if (Check->CheckpointImportance < CurrentRecord) ReturnValue.Remove(Check)
}

```

It works on anyobject that implements standard C++ iterator (begin(), end(), ++ and \* operators), which TArray and other containers in UE4 does. But this should not be used with item remove

---

<div class="post-metadata">

**Author:** ![Dirt113](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dirt113](https://forums.unrealengine.com/u/Dirt113)\
**Post date:** [November 28, 2015, 4:58am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/3 "2015-11-28T04:58:37Z")

</div>

Thanks for the answer, but I put in int32 ArrayLength as a failsafe for mid-iteration value change of Num()

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [November 28, 2015, 5:17am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/4 "2015-11-28T05:17:34Z")

</div>

Oh that might be a issue with that Remove indeed, but try 2nd solution it uses iterator in TArray insted of standard loop

---

<div class="post-metadata">

**Author:** ![Dirt113](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Dirt113](https://forums.unrealengine.com/u/Dirt113)\
**Post date:** [November 28, 2015, 5:19am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/5 "2015-11-28T05:19:46Z")

</div>

Cool, it works 😃

---

<div class="post-metadata">

**Author:** ![birdfreeyahoo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@birdfreeyahoo](https://forums.unrealengine.com/u/birdfreeyahoo)\
**Post date:** [December 7, 2017, 11:36am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/6 "2017-12-07T11:36:28Z")

</div>

Removing while using a ranged-for-loop will likely work as expected with TArray, but it is actually undefined behaviour. UE4 will trigger a breakpoint if in debug mode complaining about array has changed while iteration.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [December 9, 2017, 2:12am UTC](https://forums.unrealengine.com/t/tarray-iteration/339912/7 "2017-12-09T02:12:13Z")

</div>

You right i actully that recently too, but first option should work if you save ReturnValue.Num() beforehand. i will edit anwser
