STL Collection Types
Each STL collection type has its own template parameters, which will be discussed later. What type of collection you use is up to your needs and tastes. From past experience, the vector and map classes are the most useful. The vector class is ideal for simple and complex collection types, while the map class is used when an associative type of collection is needed. The deque collection is excellent for use in systems that have queued based processing, such as a message based system.
vector
- A collection of elements of type T.
list- A collection of elements of type T. The collection is stored as a bi-directional linked list of elements, each containing a member of type T.
dequeTo include the class definition use:
#include <list>
- A collection of varying length of elements of type T. The sequence is represented in a way that permits insertion and removal of an element at either end with a single element copy, and supports insertion and removal anywhere in the sequence, but a sequence copy follows each operation.
mapTo include the class definition use:
#include <deque>
- A collection for a varying length sequence of elements of type pair<const Key, T>. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element. The key can be a simple as a number or string or as complex a key class. The key class must support normal comparison operations so that the collection can be sorted or searched.
setTo include the class definition use:
#include <map>
- A collection that controls a varying length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.
multimapTo include the class definition use:
#include <set>
- A collection of a varying length sequence of elements of type pair<const Key, T>. The first element of each pair is the sort key and the second is its associated value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.
multisetTo include the class definition use:
#include <map>
- A collection of a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element.
To include the class definition use:
#include <set>
STL Strings
STL strings support both ascii and unicode character strings.
string
- A string is a collection of ascii characters that supports both insertion and removal.
wstringTo include the string class definitions use:
#include <string>
- A wstring is a collection of wide characters that it supports both insertion and removal. In MFC the string class is CString, which provides a Format and other methods to manipulate the string. CString has the advantage of providing methods such as Format, TrimLeft, TrimRight and LoadString. It is easy to provide a string-based class that contains these methods.
To include the wstring class definitions use:
#include <xstring>
STL Streams
Streams provide the developer with classes that can output to a container variable types of stream elements.
stringstream
- A string stream that supports insertions of elements, and elements are inserted via the overloaded operator <<. The method str() gives a reference back to the underlying string, and the c_str() can be used to get a constant pointer to the string buffer.
wstringstream- A wstring stream that supports insertions of elements, and elements are inserted via the overloaded operator <<. The method str() gives a reference back to the underlying string, and the c_str() can be used to get a constant pointer to the string buffer.
To use a string stream we would do the follwing:
stringstream strStr;
for ( long i=0; i< 10; i++ )
strStr << "Element " << i << endl;
To include the string class definitions use:
#include <strstring>