Analysis of Commonly Used Data Structures and Algorithms in Game Development "Transition from C Language to C++ and C++ Programming in Unreal Engine" Tutorial⑨

foreword

This article briefly introduces the most basic data structures and algorithms commonly used in game development. The content is relatively simple and does not involve the complex content required for data structure and algorithm competitions. It only introduces the basic concepts and serves as a catalog.

array

The concept of an array

Array (Array) is an ordered sequence of elements. [1] If a collection of finite variables of the same type is named, then this name is an array name. The individual variables that make up an array are called the components of the array, also called the elements of the array, and sometimes called subscript variables. The numerical numbers used to distinguish individual elements of an array are called subscripts. Array is a form in which several elements of the same type are organized in an orderly form for the convenience of processing in programming. [1] These ordered collections of data elements of the same type are called arrays.
Arrays are used to store multiple collections of the same type of data. --Baidu Encyclopedia

Arrays in C++

I believe everyone is familiar with arrays. In C language, we can create an array of int type in this way:

int a[5]={
    
    1,2,3,4,5};

Once this most primitive array is defined, its length can no longer be changed. In actual development, we also need variable-length arrays. C++ provides a vector container, which is defined in the header file vector, so that we can use variable long array.
The use case is as follows:

#include<vector>
#include <iostream>
using namespace std;
int main()
{
    
    
	vector<int> a = {
    
     1,2,3,4,5 };
	for (int i = 6; i < 10; ++i)
	{
    
    
		a.push_back(i);//使用push_back()添加元素
	}
	for (int i = 0; i < a.size(); ++i)//size()获取数组长度
	{
    
    
		cout << a[i] << endl;
	}
}

Sample output:

1
2
3
4
5
6
7
8
9

For more usage methods of vector, please be sure to read C++ vector container analysis

Arrays in Unreal Engine

A more advanced array is encapsulated in Unreal Engine, which can be used when developing in Unreal Engine. It has more functions than vector.
For how to use it, please refer to Unreal official documentation:
TArray in Unreal Engine

linked list

The concept of linked list

A linked list is a non-sequential and non-sequential storage structure on a physical storage unit, and the logical order of data elements is realized through the link order of pointers in the linked list. The linked list is composed of a series of nodes (each element in the linked list is called a node), and the nodes can be dynamically generated at runtime. Each node consists of two parts: one is a data field that stores data elements, and the other is a pointer field that stores the address of the next node. Compared with the linear list sequential structure, the operation is more complicated. Since it does not have to be stored in order, the linked list can reach the complexity of O(1) when inserting, which is much faster than another linear list sequence table, but it takes O(n) to find a node or access a specific numbered node time, and the corresponding time complexities of linear table and sequential table are O(logn) and O(1) respectively. --Baidu Encyclopedia

Linked list in C++

Recommended tutorial navigation:
C++ linked list and its creation

Some applications of linked list in game development

C language greedy snake (detailed explanation) - linked list implementation

the stack

The concept of stack

Stack (stack), also known as stack, is a linear table with limited operations. A linear list that restricts insertions and deletions to the end of the list only. This end is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack is also called pushing, pushing, or pushing. It is to put the new element on top of the top element of the stack to make it a new top element of the stack; deleting an element from a stack is also called stacking or stacking. Unstack, it is to delete the top element of the stack, so that the adjacent element becomes the new top element of the stack. --Baidu Encyclopedia

A stack is a first-in, last-out data structure. That is, the elements that are put into it earlier can only come out later; the elements that are put in later can come out earlier

Its structure diagram is as follows:

Stack structure diagram
As you can see, the stack is like a bucket, and elements can only be added or removed from the top.

Stack in C++

In the C++ standard library, the features of templates and generics have been used to help us create the data structure of the stack in the header file stack. The usage examples are as follows:

#include <stack>//C++标准库中栈的头文件
#include <iostream>
using namespace std;
int main()
{
    
    
	int a[5] = {
    
    1,2,3,4,5};
	stack<int>s;//尖括号放栈内元素类型名
	for (int i = 0; i < 5; ++i)
	{
    
    
		s.push(a[i]);//push函数让元素入栈
	}
	for (int i = 0; i < 5; ++i)
	{
    
    
		cout << s.top() << endl;//top函数返回对栈顶元素的引用
		s.pop();//pop函数删除栈顶元素
	}
	if (s.empty())//empty函数返回栈是否没有元素,如果没有元素(空栈)就返回true,有元素就返回false
	{
    
    
		cout << "栈空了" << endl;
	}
}

Output result:

5
4
3
2
1
栈空了

Some applications of stacks in game development:

UE4 UI management based on stack state machine (framework design)

queue

The concept of queue

Queue is a special kind of linear table, which is special in that it only allows delete operations at the front end of the table (front), and insert operations at the back end (rear) of the table. Like a stack, a queue is an operation subject to A linear list of constraints. The end of the insertion operation is called the tail of the queue, and the end of the deletion operation is called the head of the queue. --Baidu Encyclopedia

Structure diagram:
queue

Queues in C++

The C++ standard library provides queue in the header file queue, and the usage examples are as follows:

#include<queue>//C++标准库中队列的头文件
#include <iostream>
using namespace std;
int main()
{
    
    
	queue<int>a;//尖括号内放类型名
	for (int i = 0; i < 10; ++i)
	{
    
    
		a.push(i);//使用push()在队尾添加元素
	}
	cout << a.back() << endl;//使用back()输出队尾元素的值
	int len = a.size();//使用size()获取队列长度
	for (int i = 0; i < len; ++i)
	{
    
    
		cout << a.front() << endl;//使用front()输出队尾头元素的值
		a.pop();//使用pop()输出队头元素
	}
	if (a.empty())//使用empty()判断队列是否没有元素(队空)
	{
    
    
		cout << "队空" << endl;
	}
}

Queues in Unreal Engine

You can read this article:
UE4's queue TQueue

priority queue

The concept of priority queue

Ordinary queue is a first-in first-out data structure, elements are appended at the end of the queue and deleted from the head of the queue. In a priority queue, elements are given priority. When elements are accessed, the element with the highest priority is removed first. The priority queue has the highest level first out (first in, largest out) behavioral characteristics. Usually implemented using a heap data structure. --Baidu Encyclopedia

Priority Queue in C++

Recommended Tutorial Navigation:
C++ Priority Queue (priority_queue)

Supongo que te gusta

Origin blog.csdn.net/lifesize/article/details/128696707
Recomendado
Clasificación