Game Development Reference
In-Depth Information
Listing 15-4.
list::sort
bool Descending(int first, int second)
{
return first > second;
}
void SortList()
{
MyList myList = { 0, 1, 2, 3, 4 };
myList.sort(Descending);
for (auto& value : myList)
{
cout << value << endl;
}
cout << endl;
myList.sort();
for (auto& value : myList)
{
cout << value << endl;
}
}
The implication of this difference is that you cannot sort ranges of values within the list; you can only
sort the entire list at once.
Summary
This chapter has shown you how you can use the STL
list
template. I began by discussing the
differences between the way
array
,
vector
, and
list
store their elements in memory, along with a
basic explanation of how these differences affect the performance of the different containers.
I then covered how you could create your own very basic
list
implementation to help you
understand how the
list
behaves differently than an
array
or
vector
at a code level. Finally we
looked at how the STL
list
shares some iterator and algorithm types with
array
and
vector
, but
also how it differs with respect to the
array
operator and the
sort
method.
The next chapter covers four more STL containers:
set
,
map
,
unordered_set
, and
unordered_map
.
These containers provide slight variations on the binary search tree and hash map data structures
and are very useful when you need to sort and access very large sets of data.