Game Development Reference
In-Depth Information
Chapter 18
STL's bitset
The last STL container type object that I cover is the bitset . I say STL type because the bitset is
not strictly a container. It does not support iterators, the range-based for loops, or STL's algorithms.
A bitset is used for tracking individual boolean values in a combined set. A traditional method for
achieving the same result are flags implemented using bitwise operators and shifts.
Creating bitset Objects
A bitset is yet another template provided by the STL. A major difference between a bitset and
other STL containers is that you are not required to specify a type for the bitset . You can see in
Listing 18-1 that the only parameter taken by the template is the number of bit flags we would like
the bitset to contain.
Listing 18-1. Specializing bitset
#include <bitset>
using namespace std;
namespace
{
const unsigned int NUMBER_OF_BITS = 5;
}
using MyBitset = bitset<NUMBER_OF_BITS>;
This code allows us to create bitsets that consist of 5 bits. We could create a bitset containing
more or fewer bits by altering the value passed into the template.
181
 
Search WWH ::




Custom Search