25.1 STL Basics

I l @ ve RuBoard

In this section we take a look at the basic concepts that went into the design of the STL and how all these design elements come together to provide a very robust and flexible way of handling items.

25.1.1 Containers

The core of the STL is the container. We're already familiar with a couple of STL container types, the vector (a single-dimension array) and the stack.

The STL divides containers into sequences, which store their elements in order, and associative containers, in which elements are accessed using a key value.

The basic STL containers are:

vector

A random-access sequential container. This looks pretty much like a C++ array, but you can expand it by inserting elements into it. You can also delete elements from a vector.

deque

Similar to a vector, but it's faster at inserting and deleting elements in the middle of the container.

list

A doubly linked list. Does not allow for random access.

set

A set of items. Items in the set are unordered and unique.

multiset

A set that permits multiple items with the same value to be stored in it.

map

Also known as associative array. This is a container whose values can be looked up by a key. Because this is a template, the key and value can be almost anything. Only one value is stored for each key.

multimap

A map that allows multiple values to be stored for each key.

These containers give you a way of storing most data in almost any way you want to. Now that we've got our data stored, we need access to it.

25.1.2 Iterators

Iterators allow you to go through a container and access the data inside. One form of this is the forward iterator, which allows you to access each element from first to last. There is a reverse iterator that allows you to go the other way and a bidirectional iterator, which goes both ways. Finally, there is the random access iterator, which allows you to access any element randomly .

Not all containers support all iterator types. For example, the vector supports random access iterators, while the list container does not.

We'll see how to use iterators in the class program described later in this chapter.

25.1.3 Algorithms

We have containers to hold the data and iterators so we can access it. For example, the sort algorithm can be used to sort an ordered container such as a vector. Some of the other algorithms include:

find

Finds an item in a container

count

Counts the number of items in a container

equal

Tests to see if containers are equal

for_each

Runs each element of a container through a given function

copy

Copies a container

reverse

Reverses the elements of an ordered container

These three elements ”containers, iterators, and algorithms ”make up the STL. Now that we know the basics, let's see how to use this library in the real world.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net