Вы находитесь на странице: 1из 5

STL Containers & Iterators

Generic Containers

Container classes are building blocks used to create object oriented programs, and they
make the internals of a program much easier to construct.
A container class describes an object that holds other objects.
Container classes are so important that they were considered fundamental to early objectoriented languages.
The C++ approach to containers is based on templates. The containers in the Standard C+
+ library represent a broad range of data structures designed to work well with the standar
d algorithms and to meet common software development needs.

Standard Template Library


The standard template library (STL) contains

Containers
Iterators
Algorithms

A container is a way stored data is organized in memory, for example an arr


ay of elements. Containers in Software

A container is usually instantiated as an object of container class


A container class object encapsulates inside it a mechanism for
containing other objects
It also provides the necessary behaviour for adding, removing and accessing
the objects it contains
A container class gives the opportunity of reuse in different programs:
o this frees the programmer from having to recreate complex data
structures in every program to manage complex data structure

Some Containers Types

Sequential containers: vector, list and deque; They store elements


visible order
Associative containers: map, multimap, set and multiset
Containers Adapters: queue, priorityqueue and stack

in client-

Sequence Containers

A sequential container stores elements in a sequence. In other words each el


ement (except for the first and last

one) is preceded by one specific element and followed by another <vector>,


<list> and <deque> are sequential containers
In an ordinary C++ array the size is fixed and can not change during run
time,
it is also
tedious to
insert
or
delete elements. Advantage: quick random access
<Vector> is an expandable
array
that can shrink
or
grow in size,
but still has the
disadvantage of
inserting or
deleting elements in
the middle

The use of containers

The use of containers


They give us control over collections of objects,especially dynamic objects
Gives a simple mechanism for creating, accessing and destroying without explicitly
programming algorithms to do these operation add object,find object,remove object
and isempty
Iterator methods allow us to iterate throw the container

iterate throw the container


Algorithms in the
STL are
procedures that are
applied to containers to
process their data,
for example search for an
element in an array, or sort an array.
Iterators are a generalization
of the concept of pointers,
they point to elements in a container, for example you can
increment an
iterator to point to the next element in an array

The Standard Library provides various type-safe containers


for storing collections of related objects. The containers are
class templates; when you declare a container variable, you
specify the type of the elements that the container will
hold. Containers can be constructed with initializer lists.
They have member functions for adding and removing
elements and performing other operations.
You iterate over the elements in a container, and access
the individual elements by using iterators. You can use
iterators explicitly by using their member functions and
operators as well as global functions. You can also use them
implicitly, for example by using a range-for loop. Iterators

for all STL containers have a common interface but each


container defines its own specialized iterators.
Containers can be divided into three categories: sequence
containers, associative containers, and container adapters.

Sequence Containers
Sequence containers maintain the ordering of inserted elements
that you specify.
A

vector

container behaves like an array, but can automatically grow

as required. It is random access and contiguously stored, and length


is highly flexible. For these reasons and more, vector is the preferred
sequence container for most applications. When in doubt as to what
kind of sequence container to use, start by using a vector! For more
information, see vector Class.
An

array

container has some of the strengths of

vector

, but the

length is not as flexible. For more information, see array Class.


A

deque

(double-ended queue) container allows for fast insertions

and deletions at the beginning and end of the container. It shares


the random-access and flexible-length advantages of vector , but is
not contiguous. For more information, see deque Class.
A

list

container is a doubly linked list that enables bidirectional

access, fast insertions, and fast deletions anywhere in the container,


but you cannot randomly access an element in the container. For
more information, see list Class.
A

forward_list

version of

container is a singly linked listthe forward-access

list

. For more information, see forward_list Class.

Associative Containers

In associative containers, elements are inserted in a pre-defined


orderfor example, as sorted ascending. Unordered associative
containers are also available. The associative containers can be
grouped into two subsets: maps and sets.
A

map

, sometimes referred to as a dictionary, consists of a

key/value pair. The key is used to order the sequence, and the value
is associated with that key. For example, a map might contain keys
that represent every unique word in a text and corresponding values
that represent the number of times that each word appears in the
text. The unordered version of map is unordered_map . For more
information, see map Class and unordered_map Class.
A

set

is just an ascending container of unique elementsthe value

is also the key. The unordered version of

set

is

unordered_set

. For

more information, see set Class and unordered_set Class.


Both

map

and

set

only allow one instance of a key or element to be

inserted into the container. If multiple instances of elements are


required,
use multimap or multiset .
The
unordered
versions
are

unordered_multimap

and

unordered_multiset

see multimap Class, unordered_multimap


and unordered_multiset Class.

For

more

information,

Class, multiset

Class,

Ordered maps and sets support bi-directional iterators, and their


unordered counterparts support forward iterators.

Container Adapters

A container adapter is a variation of a sequence or associative


container that restricts the interface for simplicity and clarity.
Container adapters do not support iterators.

queue

container follows FIFO (first in, first out) semantics. The first

element pushedthat is, inserted into the queueis the first to


be poppedthat is, removed from the queue. For more information,
see queue Class.
A

priority_queue

container is organized such that the element that has

the highest value is always first in the queue. For more information,
see priority_queue Class.
A

stack

container follows LIFO (last in, first out) semantics. The last

element pushed on the stack is the first element popped. For more
information, see stack Class.
Because container adapters do not support iterators, they cannot be
used with the STL algorithms. For more information, see Algorithms.

Вам также может понравиться