RealDocs

TArray::Swap

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

Description

Swaps two elements at the given indices after verifying both indices are in bounds. Does nothing (no-op) when the two indices are equal. Internally delegates to SwapMemory.

Caveats & Gotchas

  • The bounds check uses check() which fires in Debug and Development builds but is compiled out in Shipping. Passing invalid indices in Shipping causes silent memory corruption, not a crash.
  • This swaps elements by value (raw memory exchange), not by logical identity. Any external pointers or references into the swapped slots will silently point to the wrong element after the swap.

Signature

UE_FORCEINLINE_HINT void Swap(SizeType FirstIndexToSwap, SizeType SecondIndexToSwap)

Parameters

Name Type Description Default
FirstIndexToSwap SizeType Index of the first element.
SecondIndexToSwap SizeType Index of the second element.

Return Type

void

Example

Bring a priority item to the front C++
TArray<FTask> TaskQueue;
// Find the urgent task and move it to front
if (int32 UrgentIdx = TaskQueue.IndexOfByPredicate([](const FTask& T){ return T.bUrgent; });
    UrgentIdx != INDEX_NONE)
{
    TaskQueue.Swap(0, UrgentIdx);
}

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.