STL Collections General Class Methods
empty
- Determines if the collection is empty
size- Determines the number of elements in the collection
begin- Returns a forward iterator pointing to the start of the collection. It is commonly used to iterate through a collection.
end- Returns a forward iterator pointing to one past the end of the collection. It is commonly used to test if an iterator is valid or in looping over a collection.
rbegin- Returns a backward iterator pointing to the end of the collection It is commonly used to iterate backward through a collection.
rend- Returns a backward iterator pointing to one before the start of the collection. It is commonly used to test if an iterator is valid or in looping over a collection.
clear- Erases all elements in a collection. If your collection contains pointers the elements must be deleted manually.
erase- Erase an element or range of elements from a collection. To erase simply call erase with an iterator pointing to the element or a pair of iterator show the range of elements to erase. Also, vector supports erasing based on array index.
Standard Out and Input
STL also includes classes for printing to the standard output streams. Like standard C++ the classes are cout and wcout. To use them in a console application include the file iostream. As an example:
#include <iostream>
void main ()
{
char ch;
cin >> ch;
cout << “This is the output terminal for STL” << endl;
}
Vector and Deque add and remove methods
We want to look briefly at adding/removing elements from the vector and deque collections. These collections are represented as an array, and to add an element we use the push methods with back or front depending on if we are adding at the front (start) or back (end) of an array.
The general methods are:
push_back | Add element to end of collection. |
push_front | Add an element to start of a collection. |
back | Get a reference to element at end of collection |
front | Get a reference to element at end of collection |
pop_back Remove | element at end of collection |
pop_front | Remove element at end of collection |
As an example, suppose we want to build a message processing system based on a message class:
Class Msg
{
int _type;
int _priority;
string _message;
public:
Msg ( int type, int priority, string & msg )
{ _type = type; priority = priority; _msg = msg; }
Msg ( int type, int priority, char * msg )
{ _type = type; priority = priority; _msg = msg; }
int getType () { return _type; }
int getPriority () { reutrn _priority; }
string & getMsg () {return _msg; }
};
To store the messages we would need a first in first out based collection, such as deque: typedef deque<Msg> MsgList;
To send a message we might do the following:
Msg message( 0, 0, "My Message" );
msgList.push_back(msg);
And to process message we could would do the following:
void process_msgs ()
{
bool done = false;
while ( !done )
{
// if no messages stop
if ( msgList.size() == 0 )
{ done == true; continue;}
// get msg and process
Msg & msg = msgList.front();
switch ( msg.getType() )
{ // process messages }
// remove msg from que
msgList.pop_front();
}
}
With just a few lines of code we have created a general messaging system, if we wanted an entire system we could create a simple COM server that exposed a mail interface, and that stored the messages using a message list.
Operator []
For vector, map, deque, string and wstring collections, elements are normally added using:
operator []
Access an element at a position, and for map, string and wstring supports insert of element.
A simple example of using this operator would be to decalre a list using map:
typedef map<int, string> StringList
StringList strings;
stringstream strStr
for ( long i=0; i<10; i++ )
{
stringstream strStr;
strStr << "String " << i;
strings[i] = strStr.str();
}
for ( long i=0; i<10; i++ )
{
string str = strings[5];
cout << str.c_str() << endl;
}
We have created a map, whose key is an integer, and that stores strings.
Iterators
Iterators support the access of elements in a collection. They are used throughout the STL to access and list elements in a container. The iterators for a particular collection are defined in the collection class definition. Below we list three types of iterators, iterator, reverse_iterator, and random access. Random access iterators are simply iterators that can go both forward and backward through a collection with any step value. For example using vector we could do the following:
vector<int> myVec;
vector<int>::iterator first, last;
for ( long i=0; i<10; i++ )
myVec.push_back(i);
first = myVec.begin();
last = myVec.begin() + 5;
if ( last >= myVec.end() )
return;
myVec.erase( first, last );
This code will erase the first five elements of the vector. Note, we are setting the last iterator to one past the last element we of interest, and we test this element against the return value of end (which give an iterator one past the last valid item in a collection). Always remember when using STL, to mark the end of an operation use an iterator that points to the next element after the last valid element in the operation.
The three types of iterators are:
iterator (forward iterator through collection)
- Allows a collection to be traversed in the forward direction. To use the iterator
reverse_iterator (reverse iterator through collection)for ( iterator element = begin(); element < end(); element++ )
t = (*element);Forward iterators support the following operations:
a++, ++a, *a, a = b, a == b, a != b- Allows a collection to be traversed in the reverse direction. As an example:
random access ( used by vector declared as forward and reverse_iterator)for ( reverse_iterator element = rbegin(); element < rend(); element++ )
t = (*element);All of the collections support forward iterators. Reverse iterators support the following operations:
a++, ++a, *a, a = b, a == b, a != b- Allows a collection to be traversed in either direction, and with any step value. An example would be:
for ( iterator element = begin(); element < end(); element+=2 )The vector collection supports random access iterators. Iterators are the most used type of access to the collections of STL, and they are also used to remove elements from collections. Look at the following:
t = (*element);iterator element = begin(); erase(element);This will set an iterator to the first element of the collection and then remove it from the collection. If we were using a vector we could do the following
iterator firstElement = begin();
iterator lastElement = begin() + 5;
erase(firstElement,lastElement);to remove the first five elements of a collection.
Random access iterators support the following:
a++, ++a, a--, --a, a += n, a -= n, a – n, a + n*a, a[n],
a = b, a == b, a != b, a < b, a <= b, a > b, a >= b
It is important to remember, when you get an iterator to a collection do not modify the collection and then expect to use the iterator. Once a collection has been modified an iterator in most cases will become invalid.