11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

366Part V: Optional FeaturesThe removeSpaces() method removes any spaces found within the stringprovided. It does this by using the string.find() operation to return theoffset of the first “ ” that it finds. Once found, removeSpaces() uses theerase() method to remove the space. The find() method returns an offsetof –1 when no more spaces are left.The type size_t is defined within the STL include files as an integer that canhandle the largest array index possible on your machine. This is typically along of some type; however, the size_t is used to further source code portabilitybetween computers. Visual Studio C++.NET will generate a warning ifyou use int instead.The insertPhrase() method uses the find() method to find the insertionpoint. It then calls erase to remove the “” flag and the string.insert()to insert a new string within the middle of an existing string.The resulting output is as follows:string1 + string2 = string1 string2 minus spaces = Stephen Davis -> Stephen Randall DavisPress any key to continue . . .The list ContainersThe Standard Template Library provides a large number of containers —many more than I can describe in a single session. However, I provide here adescription of two of the more useful families of containers.The STL list container retains objects by linking them together like Legoblocks. Objects can be snapped apart and snapped back together in anyorder. This makes the list ideal for inserting objects, sorting, merging, andotherwise rearranging objects. The following example STLList program usesthe list container to sort a set of names:// STLList - use the list container of the// Standard Template Library to input// and sort a string of names#include #include #include #include #include // declare a list of string objectsusing namespace std;list names;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!