TArray::Find
#include "Containers/Array.h"
Access: public
Specifiers: const
Description
Searches for Item using operator== and returns true if found, setting Index to its position. Returns false if not found.
Signature
bool Find(const ElementType& Item, SizeType& Index) const Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| Item | const ElementType& | The value to search for. | — |
| Index | int32& | Set to the index of the first match if found; unmodified if not found. | — |
Return Type
bool Caveats & Gotchas
- • Index is only written when the item is found — it is not set to -1 or INDEX_NONE on failure. Always check the return value before using Index.
- • Linear search O(n). For repeated lookups on large arrays, consider building a TMap or TSet.
- • There is also a single-argument overload that returns INDEX_NONE on failure instead of a bool — useful for inline index checks: int32 Idx = Array.Find(Item);
Example
Find an item and act on its index C++
int32 FoundIndex;
if (Inventory.Find(SearchItem, FoundIndex))
{
UE_LOG(LogTemp, Log, TEXT("Found at index %d"), FoundIndex);
Inventory.RemoveAt(FoundIndex);
}
// Single-argument overload returning INDEX_NONE on failure:
int32 Idx = Inventory.Find(SearchItem);
if (Idx != INDEX_NONE)
{
Inventory.RemoveAt(Idx);
} Tags
Version History
Introduced in: unknown
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?