TSet<T>

A TSet<int> variable stores a set of integers. A TSet<FString> variable. stores a set of strings. The main difference between TSet and TArray is that TSet does not allow duplicates—all the elements inside a TSet are guaranteed to be unique. A TArray variable does not mind duplicates of the same elements.

To add numbers to TSet, simply call Add. Take an example of the following declaration:

TSet<int> set;
set.Add( 1 );
set.Add( 2 );
set.Add( 3 );
set.Add( 1 );// duplicate! won't be added
set.Add( 1 );// duplicate! won't be added

This is how TSet will look, as shown in the following figure:

TSet<T>

Duplicate entries of the same value in the TSet will not be allowed. Notice how the entries in a TSet aren't numbered, as they were in a TArray: you can't use square brackets to access an entry in TSet arrays.

Iterating a TSet

In order to look into a TSet array, you must use an iterator. You can't use square brackets notation to access the elements of a TSet:

int count = 0;	// keep track of numerical index in set
for( TSet<int>::TIterator it = set.CreateIterator(); it; ++it )
{
  GEngine->AddOnScreenDebugMessage( count++, 30.f, FColor::Red,  FString::FromInt( *it ) );
}

Intersecting TSet

The TSetarray has two special functions that the TArray variable does not. The intersection of two TSet arrays is basically the elements they have in common. If we have two TSet arrays such as X and Y and we intersect them, the result will be a third, new TSet array that contains only the elements common between them. Look at the following example:

TSet<int> X;
X.Add( 1 );
X.Add( 2 );
X.Add( 3 );
TSet<int> Y;
Y.Add( 2 );
Y.Add( 4 );
Y.Add( 8 );
TSet<int> common = X.Intersect(Y); // 2

The common elements between X and Y will then just be the element 2.

Unioning TSet

Mathematically, the union of two sets is when you basically insert all the elements into the same set. Since we are talking about sets here, there won't be any duplicates.

If we take the X and Y sets from the previous example and create a union, we will get a new set, as follows:

TSet<int> uni = X.Union(Y); // 1, 2, 3, 4, 8

Finding TSet

You can determine whether an element is inside a TSet or not by using the Find() member function on the set. The TSet will return a pointer to the entry in the TSet that matches your query if the element exists in the TSet, or it will return NULL if the element you're asking for does not exist in the TSet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194