C++ STL versions of commonly used containers

I want to cover the C++ STL versions of a couple of containers. STL is the standard template library, which is shipped with most C++ compilers. The reason why I want to cover these STL versions is that they behave somewhat differently than the UE4 versions of the same containers. In some ways, their behavior is very good, but game programmers often complain of STL having performance issues. In particular, I want to cover STL's set and map containers.

Note

If you like STL's interface but want better performance, there is a well-known reimplementation of the STL library by Electronic Arts called EASTL, which you can use. It provides the same functionality as STL but is implemented with better performance (basically by doing things such as eliminating bounds checking). It is available on GitHub at https://github.com/paulhodge/EASTL.

C++ STL set

A C++ set is a bunch of items that are unique and sorted. The good feature about the STL set is that it keeps the set elements sorted. A quick and dirty way to sort a bunch of values is actually to just shove them into the same set. The set will take care of the sorting for you.

We can return to a simple C++ console application for the usage of sets. To use the C++ STL set you need to include <set>, as shown here:

#include <iostream>
#include <set>
using namespace std;

int main()
{
  set<int> intSet;
  intSet.insert( 7 );
  intSet.insert( 7 );
  intSet.insert( 8 );
  intSet.insert( 1 );

  for( set<int>::iterator it = intSet.begin(); it != intSet.end();  ++it )
  {
    cout << *it << endl;
  }
}

The following is the output of the preceding code:

1
7
8

The duplicate 7 is filtered out, and the elements are kept in increasing order inside the set. The way we iterate over the elements of an STL container is similar to UE4's TSet array. The intSet.begin() function returns an iterator that points to the head of the intSet.

The condition to stop iterating is when it becomes intSet.end(). intSet.end() is actually one position past the end of the set, as shown in the following figure:

C++ STL set

Finding an element in a <set>

To find an element inside an STL set, we can use the find() member function. If the item we're looking for turns up in the set, we get an iterator that points to the element we were searching for. If the item that we were looking for is not in the set, we get back set.end()instead, as shown here:

set<int>::iterator it = intSet.find( 7 );
if( it != intSet.end() )
{
  //  7  was inside intSet, and *it has its value
  cout << "Found " << *it << endl;
}

Exercise

Ask the user for a set of three unique names. Take each name in, one by one, and then print them in a sorted order. If the user repeats a name, then ask them for another one until you get to three.

Solution

The solution of the preceding exercise can be found using the following code:

#include <iostream>
#include <string>
#include <set>
using namespace std;
int main()
{
  set<string> names;
  // so long as we don't have 3 names yet, keep looping,
  while( names.size() < 3 )
  {
    cout << names.size() << " names so far. Enter a name" << endl;
    string name;
    cin >> name;
    names.insert( name ); // won't insert if already there,
  }
  // now print the names. the set will have kept order
  for( set<string>::iterator it = names.begin(); it !=  names.end(); ++it )
  {
    cout << *it << endl;
  }
}

C++ STL map

The C++ STL map object is a lot like UE4's TMap object. The one thing it does that TMap does not is to maintain a sorted order inside the map as well. Sorting introduces an additional cost, but if you want your map to be sorted, opting for the STL version might be a good choice.

To use the C++ STL map object, we include <map>. In the following example program, we populate a map of items with some key-value pairs:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
  map<string, int> items;
  items.insert( make_pair( "apple", 12 ) );
  items.insert( make_pair( "orange", 1 ) );
  items.insert( make_pair( "banana", 3 ) );
  // can also use square brackets to insert into an STL map
  items[ "kiwis" ] = 44;

  for( map<string, int>::iterator it = items.begin(); it !=  items.end(); ++it )
  {
    cout << "items[ " << it->first << " ] = " << it->second <<  endl;
  }
}

This is the output of the preceding program:

items[ apple ] = 12
items[ banana ] = 3
items[ kiwis ] = 44
items[ orange ] = 1

Notice how the iterator's syntax for an STL map is slightly different than that of TMap: we access the key using it->first and the value using it->second.

Notice how C++ STL also offers a bit of syntactic sugar over TMap; you can use square brackets to insert into the C++ STL map. You cannot use square brackets to insert into a TMap.

Finding an element in a <map>

You can search a map for a <key, value> pair using the STL map's find member function.

Exercise

Ask the user to enter five items and their quantities into an empty map. Print the results in sorted order.

Solution

The solution of the preceding exercise uses the following code:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
  map<string, int> items;
  cout << "Enter 5 items, and their quantities" << endl;
  while( items.size() < 5 )
  {
    cout << "Enter item" << endl;
    string item;
    cin >> item;
    cout << "Enter quantity" << endl;
    int qty;
    cin >> qty;
    items[ item ] = qty; // save in map, square brackets
    // notation
  }

  for( map<string, int>::iterator it = items.begin(); it !=  items.end(); ++it )
  {
    cout << "items[ " << it->first << " ] = " << it->second <<  endl;
  }
}

In this solution code, we start by creating map<string, int> items to store all the items we're going to take in. Ask the user for an item and a quantity; then we save the item in the items map using square brackets notation.

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

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