10.6. Standard C++ Library and STL

With the removal of the IBM Open Class (IOC) Library in Version 6, the VisualAge C++ for AIX compiler has standardized on the use of the Standard C++ Library, including the Standard Template Library (STL).

When migrating from a previous version of the VisualAge C++ for AIX compiler, refer to the IBM Open Class Library Transition Guide, SC09-4948 to determine whether your application uses IOC, what version of IOC it is using, and the general migration suggestions for application owners whose applications use the IOC. It also contains migration suggestions for most classes included in the IBM Open Class Library.

In general, where an overlap in functions exists between the IBM Open Class Library and the Standard C++ Library, including the Standard Template Library, the following is recommended:

  • Use the Standard Template Library (STL) containers, iterators, and algorithms instead of the IOC collection classes.

  • Use the Standard C++ exception classes instead of the IOC exception classes.

  • Use the Standard C++ string template classes instead of the IOC string classes.

However, there are many classes in the IBM Open Class Library for which there is no equivalent in the Standard C++ Library. The IBM Open Class Library Transition Guide, SC09-4948 identifies some of the options available to application owners to deal with this situation. The decision as to which option is best depends on the version of the IBM Open Class Library you use and the extent to which you use classes without an equivalent replacement in the Standard C++ Library.

10.6.1. Standard Template Library

The motivation behind the design of the Standard Template Library, or STL, is to support object-oriented programming with type generality. Implemented using templates, STL provides a set of commonly used abstract data types, access methods, and operation methods, allowing components to be easily created, and operations performed with minimal lines of code.

STL is composed of three major components: containers, iterators, and algorithms. The following sections explore each component in more detail.

Containers

A container is an abstract object data type. It is a template class that manages a sequence or set of elements. Conceptually, it is an object containing other objects, much like an ordinary array. The elements of a container can be of any object type, and the allocation and deallocation of the elements are controlled by the container methods. You can also perform operations such as insert, remove, copy, or compare elements with operators provided by the container.

STL containers come in two different varieties: sequence containers and associative containers. A sequence container is a linear block of objects, where elements are referred to by their position in the container. STL provides the following sequence containers:

VectorA contiguous block of objects that allows random access and fast insertions at the end.
ListSupports only sequential access, but has equally fast insertions and deletions from anywhere within the list.
DequeAllows random access and fast insertions and deletions from either end of the queue.
StackSpecialized container that supports stack operations only (LIFO).
QueueSpecialized container that supports queue operations only (FIFO).

Associative containers, on the other hand, do not store elements in any order, and retrieval of elements are fast by using keys. Associative container template classes are parameterized either on a key, or a key and a type. They can be based on unique keys, where there is at most one element in the container for each key. Alternatively, they can be based on equivalent keys, where there is possibly multiple copies of the same key in the container. STL provides the following associative containers:

SetSupports unique keys and fast retrieval of keys themselves.
MapSupports unique keys and fast retrieval of values (of the parameterized type) based on the keys.
MultisetSupports equivalent keys and fast retrieval of keys themselves.
MultimapSupports equivalent keys and fast retrieval of values (of the parameterized type) based on the keys.

To use a container, simply declare a variable with the desired container class. For example, to create an empty vector of integers:

vector<int> v;

To add an integer n to the vector:

v.push_back(n);

To get the size of the vector:

int size = v.size();

See the VisualAge C++ Standard C++ Library Reference, SC09-4949 for more information on the methods provided by the various container classes.

Iterators

Iterators are abstract pointers to containers, that allow you to work with the various containers in much the same way as you would with normal C++ pointers. You can refer to a location in a container using an iterator, increment it to get to the next position, dereference it to get the value of the element, and so on. Two iterators can be compared to find the relative locations with respect to each other, or subtracted to find the distance between the two locations. For example:

vector<int>::iterator start = v.begin();
vector<int>::iterator end = v.end();
int size = end - start;

the above lines of code are equivalent to:

int size = v.size();

And to print the value of the first element of the vector:

cout << *start << endl;

The standard defines five categories of iterators, according to the type of operations they perform:

InputProvides data read access to a container in a sequential manner.
OutputProvides data write access to a container in a sequential manner.
ForwardAllows forward traversal (that is, increment only) of the container in a sequential manner. This can replace an input or output iterator.
BidirectionalAllows traversal of the container in both directions (that is, increment and decrement) in a sequential manner. This can replace a forward iterator.
Random accessAllows random access of a container. This can replace a bidirectional iterator.

Refer to the VisualAge C++ Standard C++ Library Reference, SC09-4949 for more details.

Algorithms

Algorithms are template functions that are specialized to perform operations on containers. They are parameterized by iterators instead of containers, in order to support other user-defined types.

There are three main types of algorithmic operations. Non-modifying sequence operations, as the name suggests, do not alter the sequence of the container in any manner. It includes template functions such as find, count, equal, mismatch, and search.

Mutating sequence operations, including functions such as copy, swap, transform, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle, and partition, change the sequence of the container while the operation is carried out.

Sorting and related operations may or may not change the sequence, and include such functions as sort, nth_element, lower_bound, upper_bound, equal_range, binary_search, merge, and so on. There are also set operations functions like set_union, set_intersection, and set_difference that operate on sorted containers.

For more information on all available algorithmic functions, refer to the VisualAge C++ Standard C++ Library Reference, SC09-4949 for details.

10.6.2. A STL example

Putting it all together, the following is a simple example that takes an integer number as input, and randomly generates 10 numbers between 0 to 100, using the input as the seed value for the random generator function. The numbers are stored in a vector container. They are then sorted and output to the terminal:

#include <algorithm>
#include <vector>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
    unsigned int seed;
    if (argc >= 2 && (seed = atoi(argv[1])))
        srand(seed);

    vector<int> v;
    for (int i = 0; i < 10; i++)
        v.push_back(1+(int)(100.0*rand()/(RAND_MAX+1.0)));

    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset