RealDocs

TArray::RemoveSwap

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

Description

Removes all occurrences of Item using RemoveAtSwap, which fills each removed slot with the last element. Faster than Remove when element order is not important. Returns the number of elements removed.

Caveats & Gotchas

  • Order is NOT preserved — as with RemoveSingleSwap and RemoveAllSwap, removed indices are back-filled by the last element. Use Remove if order must be maintained.
  • Passing an item that is a reference into the array itself (CheckAddress is called inside) will assert. Ensure Item is not a reference to an element of this TArray.

Signature

SizeType RemoveSwap(const ElementType& Item, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
Item const ElementType& The item value to remove all instances of.
AllowShrinking EAllowShrinking Whether to allow the allocator to shrink after removal. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

SizeType

Example

Remove all occurrences of a handle value C++
TArray<int32> ActiveSlots;
int32 NumRemoved = ActiveSlots.RemoveSwap(InvalidSlot);
UE_LOG(LogTemp, Log, TEXT("Removed %d stale slots"), NumRemoved);

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.