当前位置:网站首页>Array tarray in ue4/5
Array tarray in ue4/5
2022-07-21 17:33:00 【Zero one_】
TArray yes UE To replace C++ In the standard library vector.STL Although very excellent , But in the game field , There is a problem with memory allocation , Mainly reflected in the memory utilization is not high enough 、 Slow distribution . The author once used in the shortest path algorithm vector Wait for the standard library container ,32G Direct memory overflow , Then use C Language array implementation , Memory usage 1G many . To solve the above problems ,UE Developed TArray、TMap、TSet Equal container , Replace it completely STL Medium vector、map、set etc. .
This article will summarize TArray Common usage of , in general ,TArray The usage and vector be similar , But there are also big differences .
One ,TArray The creation of 、 initialization 、 assignment
TArray The creation of 、 initialization 、 The main forms of assignment are as follows . among , Lists can be used as arguments to constructors , It can also be used for initialization and assignment .Init() Can be in TArray Put several same elements in .
TArray<FString> Arr;
//Arr is []
TArray<FString> SecArr({ TEXT("Are"), TEXT("you"), TEXT("OK") });
//SecArr is [Are you OK]
TArray<FString> ThirdArr = { TEXT("Are"), TEXT("you"), TEXT("OK") };
//ThirdArr is [Are you OK]
ThirdArr.Init(TEXT("Hello"), 3);
//ThirdArr is [Hello Hello Hello]
Arr = { TEXT("Hello"), TEXT("World") };
//Arr is [Hello World]
Two ,TArray Element addition of
Add a single element
towards TArray Add a single element , There are many ways :Add、Push、Emplace、AddUnique、Insert. In specific use , They also have some differences , It can be summed up as follows :
- The first four are to append elements at the end of the array ,Insert You can insert elements anywhere in the array ;
- Add and Push similar , But copy or move elements into the array , and Emplace Then use the given parameters to build a new instance of the element type ; Generally speaking ,Emplace Is more efficient than Add;
- If the same element doesn't exist ( Use operators = Judge ),AddUnique Will add elements to the array .
The usage of each function is as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") };
Arr.Add(TEXT(","));
//Arr is [Are you OK ,]
Arr.Push(TEXT("Hello"));
//Arr is [Are you OK , Hello]
Arr.Emplace(TEXT("World"));
//Arr is [Are you OK , Hello World]
Arr.AddUnique(TEXT(","));
//Arr is [Are you OK , Hello World]
Arr.AddUnique(TEXT("I"));
//Arr is [Are you OK , Hello World I]
Arr.Insert(TEXT("Head"), 0);
//Arr is [Head Are you OK , Hello World I]
Add multiple elements
Add multiple elements to the array , May adopt Append() Method or += The operator completes , How to use it is as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") };
TArray<FString> SecArr({ TEXT("How"), TEXT("Do"), TEXT("You") });
Arr.Append(SecArr);
//Arr is [Are you OK How Do You]
TArray<FString> ThirdArr({ TEXT("I"), TEXT("Am"), TEXT("Fine") });
Arr += ThirdArr;
//Arr is [Are you OK How Do You I Am Fine]
3、 ... and ,TArray Three kinds of traversal
TArray There are three forms of traversal : Range for loop 、 Indexes 、 iterator , On this point and vector similar . The following shows how to use these traversals .
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") };
for (auto& s : Arr)
{
UE_LOG(LogTemp, Warning, TEXT("%s"), *s);
}
for (int i = 0; i < Arr.Num(); i++)
{
UE_LOG(LogTemp, Warning, TEXT("%d: %s"), i, *Arr[i]);
}
for (auto It = Arr.CreateIterator(); It; ++It)
{
UE_LOG(LogTemp, Warning, TEXT("%s"), *(*It));
}
Four ,TArray There are three kinds of ordering of the
TArry There are also three sorting methods :Sort、HeapSort、StableSort. Their simple usage is as follows :
Arr.Sort();
Arr.HeapSort();
Arr.StableSort();
The three are not the same , There are the following differences :
- Sort Based on quick sort , It's an unstable sequence ;
- HeapSort Heap based sorting , It's an unstable sequence ;
- StableSort Sort based on merge , Is a stable sort .
By default , The above three sorting methods all use element types < Operator to sort . We can use lambda Custom collation , as follows , We pair according to the length of the string FString Sort .
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") ,TEXT("Hello"), TEXT("World") };
Arr.StableSort(
[](const FString& A, const FString& B) {return A.Len() < B.Len(); }
);
//Arr is [OK Are you Hello World]
5、 ... and ,TArray Delete the elements of
There are many ways to delete elements , Yes Remove()、RemoveSingle()、RemoveAll()、RemoveAt()、Empty(). Sum up , They are used as follows :
- Remove(Str): Delete all and in the array Str Same element , Is it the same == Operator judgment ;
- RemoveSingle(Str): Delete the first and... In the array Str Same element , Is it the same == Operator judgment ;
- RemoveAll(lambda): Delete multiple elements in the array , Delete rule by lambda Expression assignment ;
- RemoveAt(i): Delete the array with index i The elements of ;
- Empty(): It is not to judge that the array is empty , Instead, empty all elements ;
- Remove(Str) And RemoveSingle(Str) The difference is to delete a single element , Or all the elements ;
- Remove(Str) And RemoveAll(lambda) The difference lies in the deletion rules .
The code is shown as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
Arr.Remove(TEXT("OK"));
//Arr is [Are you]
Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
Arr.RemoveSingle(TEXT("OK"));
//Arr is [Are you OK]
Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
Arr.RemoveAll([](const FString& Str) {return Str.Len() == 2; });
//Arr is [Are you]
Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
Arr.RemoveAt(0);
//Arr is [you OK OK]
Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
Arr.Empty();
//Arr is []
6、 ... and ,TArray Query for
Get the reference of the element object according to the index :[] Operator 、Last()、Top()
Use [] Operator 、Last()、Top() Can get the elements in the array according to the index , And all we get are references to elements , That is to say, we can directly modify the elements in the array . As shown in the following code , perform Arr[0] = Arr[1] after , The first 0 The value of the first element also becomes the second 1 Elements ; perform Arr.Top() = Arr[1] after , The value of the last element also becomes the 1 Elements . The connections and differences between these three methods are as follows :
- Arr[i] What you get is the order i Elements ;
- Last(i) Get the penultimate i Elements ;
- Top() Cannot take parameter , The actual effect is completely consistent with Last() equally .
The specific usage of the three methods is as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") ,TEXT("Hello"), TEXT("World") };
Arr[0] = Arr[1];
// Arr is [you you OK Hello World]
UE_LOG(LogTemp, Warning, TEXT("%s"), *Arr.Last()); //Output: World
UE_LOG(LogTemp, Warning, TEXT("%s"), *Arr.Last(1)); //Output: Hello
UE_LOG(LogTemp, Warning, TEXT("%s"), *Arr.Top()); //Output: World
Arr.Top() = Arr[1];
// Arr [you you OK Hello you]
Determine whether an element exists :Contains()、ContainsByPredicate()
Determine whether an element exists in the array , The way to do it is Contains() And its variants ContainsByPredicate().ContainsByPredicate() It is allowed to use unary predicates to specify judgment rules . The usage is as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK") ,TEXT("Hello"), TEXT("World") };
bool bExist = Arr.Contains(TEXT("Are"));
bool bContain = Arr.ContainsByPredicate(
[](const FString& Str) {return Str.Len() == 3; }
);
Find the index of the element :Find()、FindLast()、IndexOfByKey()、IndexOfByPredicate()
Find the index of the element , The way to do it is Find()、FindLast()、IndexOfByKey()、IndexOfByPredicate(). If the search is successful , Then return the index of the element ; To find the failure , Then return to INDEX_NONE( The value is -1 The macro ). Their usage is summarized as follows :
- Find(Str): Returns the first and in the array Str Index of the same element , Is it the same == Operator judgment ;
- FindLast(Str): Returns the last and in the array Str Index of the same element , Is it the same == Operator judgment ;
- IndexOfByKey(Str): And Find(Str) equally ;
- IndexOfByPredicate(lambda):IndexOfByKey Of lambda edition , use lambda Expression to specify the rules for finding elements .
The code example is as follows :
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
int Index = Arr.Find(TEXT("OK"));
//Index is 2
Index = Arr.Find(TEXT("I"));
//Index is INDEX_NONE
Index = Arr.FindLast(TEXT("OK"));
//Index is 3
Index = Arr.IndexOfByKey(TEXT("OK"));
//Index is 2
Index = Arr.IndexOfByPredicate([](const FString& Str) {return Str.Len() == 3; });
//Index is 0
Find element object :FindByKey()、FindByPredicate()、FilterByPredicate()
FindByKey()、FindByPredicate()、FilterByPredicate() It can be used to get the element object in the array .FindByKey()、FindByPredicate() All we get are pointers to elements ,FilterByPredicate() What you get is all the elements that match the criteria , The usage is summarized as follows :
- FindByKey(Str): Get the first and... In the array Str Pointer to the same element , Get failed return nullptr;
- FindByPredicate(lambda):FindByKey Of lambda edition , use lambda Expression to specify the rules for finding elements ;
- FilterByPredicate(lambda): Get all the causes in the array lambda Expression for true The elements of , But these elements are copies , Returns an array .
TArray<FString> Arr = { TEXT("Are"), TEXT("you"), TEXT("OK"), TEXT("OK") };
FString* StrPtr = Arr.FindByKey(TEXT("OK"));
//StrPtr Is a string pointing to "OK" The pointer to
StrPtr = Arr.FindByPredicate([](const FString& Str) {return Str.Len() == 3; });
//StrPtr Is a string pointing to "Are" The pointer to
TArray<FString> StrArr = Arr.FilterByPredicate([](const FString& Str) {return Str.Len() == 3; });
//StrArr is [Are you]
边栏推荐
- Data analysis demo
- Starbucks CEO said that more stores may be closed to ensure the safety of employees
- Swift 中风味各异的依赖注入
- cv demo
- Opencv learning - aruco module
- 在线博客系统设计
- 支持向量机进行枣类遗传的回归预测分析
- Analysis of tars source code 24
- Traverse the pictures of folders and subfolders, use OpenCV to change the size and save to a folder
- C#(四十一)之线程
猜你喜欢
随机推荐
华为无线设备配置同一业务VLAN的AP间快速漫游
mysql 常用的时间相关操作
Opencv learning - aruco module
The difference between memory overflow and memory leak
cv demo
TZC 1283: 简单排序 —— 归并排序
MySQL explain execution plan analysis
Regression prediction analysis of jujube genetics based on support vector machine
封装全局input组件,并通过v-model绑定父子数据
SAP Fiori topic 2: using webide to build Fiori with navigation bar
Swift 中风味各异的依赖注入
Huawei wireless device configuration traffic supervision
星巴克CEO称可能关闭更多店面以保证员工安全
ARIMA疫情期间港口靠挂数的时间序列分析
C#(三十三)之路径(Path)
Replication of complex linked list
OpenCV学习——ArUco模块
(c语言)柔性数组
同城两中心自适应同步模式部署
C#(四十一)之线程