RealDocs

TArray::RemoveAtSwap

function Core Since 4.0
#include "Containers/Array.h"
Access: public Specifiers: inline

Description

Removes the element at Index by filling its slot with element(s) from the end of the array. O(Count) rather than O(N), but does not preserve element order.

Caveats & Gotchas

  • Element order is NOT preserved — the gap is filled by relocating elements from the tail. Never use this when iteration order or sorted order matters.
  • For a single-element removal (default), one element is moved from the end into the removed slot. For a Count-based removal, as many tail elements as needed are relocated — but the resulting tail region is not necessarily in any meaningful order.
  • The bool bAllowShrinking overload is deprecated in UE 5.6. Use EAllowShrinking::No or EAllowShrinking::Yes.
  • All raw pointers into the array are potentially invalidated even if no reallocation occurs, because the tail element is relocated.

Signature

UE_FORCEINLINE_HINT void RemoveAtSwap(SizeType Index, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
Index SizeType Index of the element to remove.
AllowShrinking EAllowShrinking Controls whether the backing allocation is shrunk after removal. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

void

Example

Order-independent removal from a large pool C++
TArray<AActor*> ActorPool;
// ... populate pool ...

// Remove a dead actor cheaply (O(1), no order needed)
int32 DeadIndex = ActorPool.IndexOfByKey(DeadActor);
if (DeadIndex != INDEX_NONE)
{
    ActorPool.RemoveAtSwap(DeadIndex, EAllowShrinking::No);
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.