< Previous | Contents | Next >

The Standard Template Library: Hangman


So far youve seen how to work with sequences of values using arrays. But there are more sophisticated ways to work with collections of values. In fact, working with collections is so common that part of standard C++ is dedicated to doing just that. In this chapter, youll get an introduction to this important library. Specifically, youll learn to:

n Use vector objects to work with sequences of values

n Use vector member functions to manipulate sequence elements

n Use iterators to move through sequences

n Use library algorithms to work with groups of elements

n Plan your programs with pseudocode


Introducing the Standard Template Library Good game programmers are lazy. Its not that they dont want to work; its just that they dont want to redo work thats already been doneespecially if it has

been done well. The STL (Standard Template Library) represents a powerful

collection of programming work thats been done well. It provides a group of containers, algorithms, and iterators, among other things.

So whats a container and how can it help you write games? Well, containers let you store and access collections of values of the same type. Yes, arrays let you do


115

116 Chapter 4 n The Standard Template Library: Hangman


the same thing, but the STL containers offer more flexibility and power than a simple but trusty array. The STL defines a variety of container types; each works in a different way to meet different needs.

The algorithms defined in the STL work with its containers. The algorithms are common functions that game programmers find themselves repeatedly applying to groups of values. They include algorithms for sorting, searching, copying, merging, inserting, and removing container elements. The cool thing is that the same algorithm can work its magic on many different container types.

Iterators are objects that identify elements in containers and can be manipulated to move among elements. Theyre great for, well, iterating through containers. In addition, iterators are required by the STL algorithms.

All of this makes a lot more sense when you see an actual implementation of one of the container types, so thats up next.


 

Using VectorsIntroducing the Hero’s Inventory 2.0 ProgramPreparing to Use VectorsDeclaring a VectorUsing the push_back() Member FunctionUsing the size() Member FunctionIndexing VectorsCalling Member Functions of an ElementUsing the pop_back() Member FunctionUsing the clear() Member FunctionUsing the empty() Member FunctionUsing IteratorsIntroducing the Hero’s Inventory 3.0 ProgramDeclaring IteratorsLooping through a VectorChanging the Value of a Vector ElementAccessing Member Functions of a Vector ElementUsing the insert() Vector Member FunctionUsing the erase() Vector Member FunctionUsing AlgorithmsIntroducing the High Scores ProgramPreparing to Use AlgorithmsUsing the find() AlgorithmUsing the random_shuffle() AlgorithmUsing the sort() AlgorithmUnderstanding Vector PerformanceExamining Vector GrowthExamining Element Insertion and DeletionExamining Other STL ContainersPlanning Your ProgramsUsing PseudocodeUsing Stepwise RefinementIntroducing HangmanPlanning the GameSetting Up the ProgramInitializing Variables and ConstantsEntering the Main LoopGetting the Player’s GuessEnding the GameSummaryQuestions and AnswersDiscussion QuestionsExercises