RealDocs

TArray::RemoveAllSwap

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

Description

Removes all elements satisfying Predicate using RemoveAtSwap internally, which replaces each removed element with the last element rather than shifting. This is faster than RemoveAll when element order is not important.

Caveats & Gotchas

  • Element order is NOT preserved — removed positions are filled by swapping in the last element. Never use this when the array's iteration order has semantic meaning (e.g. a priority queue or an ordered list rendered to UI).
  • The predicate is called once per element in a simple forward pass with no run-length optimisation. RemoveAll (order-preserving) uses a more cache-friendly run-length pass and may be faster on arrays where most elements are removed.

Signature

template <class PREDICATE_CLASS>
SizeType RemoveAllSwap(const PREDICATE_CLASS& Predicate, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
Predicate const PREDICATE_CLASS& A callable that returns true for elements to remove.
AllowShrinking EAllowShrinking Whether to allow the allocator to shrink after removal. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

SizeType

Example

Remove all dead actors from a spawn list C++
TArray<AActor*> SpawnedActors;
SpawnedActors.RemoveAllSwap([](AActor* A)
{
    return !IsValid(A);
});

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.