TArrayView

Templated fixed-size view of another array

Windows
MacOS
Linux

References

Module

Core

Header

/Engine/Source/Runtime/Core/Public/Containers/ArrayView.h

Include

#include "Containers/ArrayView.h"

Syntax

template<typename InElementType>
class TArrayView

Remarks

Templated fixed-size view of another array

A statically sized view of an array of typed elements. Designed to allow functions to take either a fixed C array or a TArray with an arbitrary allocator as an argument when the function neither adds nor removes elements

e.g.: int32 SumAll(TArrayView<const int32> array) { return Algo::Accumulate(array); }

could be called as: SumAll(MyTArray);\ SumAll(MyCArray); SumAll(MakeArrayView(Ptr, Num));

auto Values = { 1, 2, 3 }; SumAll(Values);

Note: View classes are not const-propagating! If you want a view where the elements are const, you need "TArrayView<const T>" not "const TArrayView<T>"! TArrayViews cannot be implicitly constructed from an rvalue initializer lists:

, as a compile-time guard against :

SumAll({ 1, 2, 3 }); // error

Caution: Treat a view like a reference to the elements in the array. DO NOT free or reallocate the array while the view exists! For this reason, TArrayViews are not constructible from rvalue initializer lists:

SumAll({ 1, 2, 3 }); // error

This is to avoid this issue:

TArrayView<int> View = { 1, 2, 3 }; // construction of array view from rvalue initializer list int n = View[0]; // undefined behavior, as the initializer list was destroyed at the end of the previous line

Constructors

Name Description

Public function

TArrayView()

Constructor.

Public function

TArrayView

(
    OtherRangeType&& Other
)

Constructor from another range

Public function

TArrayView

(
    std::initializer_list< ElementType ...
)

Construct a view of an initializer list.

Public function

TArrayView

(
    OtherElementType* InData,
    int32 InCount
)

Construct a view of an arbitrary pointer

Functions

Name Description

Public function Const

ElementType ...

 

begin()

DO NOT USE DIRECTLY STL-like iterators to enable range-based for loop support.

Public function Const

void

 

CheckInvariants()

Checks array invariants: if array size is greater than zero and less than maximum.

Public function Const

bool

 

Contains

(
    const ComparisonType& Item
)

Checks if this array contains the element.

Public function Const

bool

 

ContainsByPredicate

(
    Predicate Pred
)

Checks if this array contains element for which the predicate is true.

Public function Const

ElementType ...

 

end()

Public function Const

TArray< type...

 

FilterByPredicate

(
    Predicate Pred
)

Filters the elements in the array based on a predicate functor.

Public function Const

bool

 

Find

(
    const ElementType& Item,
    int32& Index
)

Finds element within the array.

Public function Const

int32

 

Find

(
    const ElementType& Item
)

Finds element within the array.

Public function Const

ElementType ...

 

FindByKey

(
    const KeyType& Key
)

Finds an item by key (assuming the ElementType overloads operator== for the comparison).

Public function Const

ElementType ...

 

FindByPredicate

(
    Predicate Pred
)

Finds an element which matches a predicate functor.

Public function Const

bool

 

FindLast

(
    const ElementType& Item,
    int32& Index
)

Finds element within the array starting from the end.

Public function Const

int32

 

FindLastByPredicate

(
    Predicate Pred,
    int32 StartIndex
)

Finds element within the array starting from StartIndex and going backwards.

Public function Const

int32

 

FindLastByPredicate

(
    Predicate Pred
)

Finds element within the array starting from the end. Uses predicate to match element.

Public function Const

ElementType ...

 

GetData()

Helper function for returning a typed pointer to the first array entry.

Public function Const

size_t

 

GetTypeSize()

Helper function returning the size of the inner type.

Public function Const

int32

 

IndexOfByKey

(
    const KeyType& Key
)

Finds an item by key (assuming the ElementType overloads operator== for the comparison).

Public function Const

int32

 

IndexOfByPredicate

(
    Predicate Pred
)

Finds an item by predicate.

Public function Const

bool

 

IsValidIndex

(
    int32 Index
)

Tests if index is valid, i.e. than or equal to zero, and less than the number of elements in the array.

Public function Const

ElementType ...

 

Last

(
    int32 IndexFromTheEnd
)

Returns n-th last element from the array.

Public function Const

int32

 

Num()

Returns number of elements in array.

Public function Const

void

 

RangeCheck

(
    int32 Index
)

Checks if index is in array range.

Public function Const

TArrayView

 

Slice

(
    int32 Index,
    int32 InNum
)

Returns a sliced view

Public function

void

 

Sort()

Sorts the array assuming < operator is defined for the item type.

Public function

void

 

Sort

(
    const PREDICATE_CLASS& Predicate
)

Sorts the array using user define predicate class.

Public function

void

 

StableSort()

Stable sorts the array assuming < operator is defined for the item type.

Public function

void

 

StableSort

(
    const PREDICATE_CLASS& Predicate
)

Stable sorts the array using user defined predicate class.

Operators

Name Description

Public function Const

ElementType ...

 

operator[]

(
    int32 Index
)

Array bracket operator. Returns reference to element at give index.

Typedefs

Help shape the future of Unreal Engine documentation! Tell us how we're doing so we can serve you better.
Take our survey
Dismiss