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

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. for example search for an element in an array. it is also tedious to insert or delete elements. 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 iterate over the elements in a container.  one) is preceded by one specific element and followed by another <vector>. they point to elements in a container. <list> and <deque> are sequential containers In an ordinary C++ array the size is fixed and can not change during run time. Iterators are a generalization of the concept of pointers. accessing and destroying without explicitly programming algorithms to do these operation add object. They have member functions for adding and removing elements and performing other operations.especially dynamic objects  Gives a simple mechanism for creating.find object. The containers are class templates. for example by using a range-for loop. you specify the type of the elements that the container will hold. Iterators . Advantage: quick random access  <Vector> is an expandable array that can shrink or grow in size. 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. You can also use them implicitly. or sort an array.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. Containers can be constructed with initializer lists. when you declare a container variable.

A forward_list version of container is a singly linked list—the forward-access list . but the length is not as flexible. and container adapters. A vector container behaves like an array. and length is highly flexible. When in doubt as to what kind of sequence container to use. vector is the preferred sequence container for most applications. For more information. see list Class. For more information. associative containers. see vector Class.for all STL containers have a common interface but each container defines its own specialized iterators. and fast deletions anywhere in the container. see forward_list Class. It shares the random-access and flexible-length advantages of vector . A list container is a doubly linked list that enables bidirectional access. Sequence Containers Sequence containers maintain the ordering of inserted elements that you specify. start by using a vector! For more information. An array container has some of the strengths of vector . It is random access and contiguously stored. but can automatically grow as required. For these reasons and more. fast insertions. For more information. see array Class. Containers can be divided into three categories: sequence containers. A deque (double-ended queue) container allows for fast insertions and deletions at the beginning and end of the container. For more information. but is not contiguous. see deque Class. but you cannot randomly access an element in the container. Associative Containers .

The unordered version of set is unordered_set . as sorted ascending. The unordered versions are unordered_multimap and unordered_multiset . A set is just an ascending container of unique elements—the value is also the key. elements are inserted in a pre-defined order—for example. use multimap or multiset . For more information. unordered_multimap and unordered_multiset Class. see multimap Class. see map Class and unordered_map Class. For more information. 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. multiset Class. The associative containers can be grouped into two subsets: maps and sets. Both map and set only allow one instance of a key or element to be inserted into the container. Class. A map . If multiple instances of elements are required. and their unordered counterparts support forward iterators.In associative containers. and the value is associated with that key. Unordered associative containers are also available. consists of a key/value pair. Ordered maps and sets support bi-directional iterators. . For more information. see set Class and unordered_set Class. The key is used to order the sequence. Container Adapters A container adapter is a variation of a sequence or associative container that restricts the interface for simplicity and clarity. sometimes referred to as a dictionary. Container adapters do not support iterators. For example. The unordered version of map is unordered_map .

A queue container follows FIFO (first in. inserted into the queue—is the first to be popped—that is. see stack Class. first out) semantics. For more information. A stack container follows LIFO (last in. For more information. A priority_queue container is organized such that the element that has the highest value is always first in the queue. see queue Class. first out) semantics. The first element pushed—that is. . For more information. removed from the queue. For more information. Because container adapters do not support iterators. see Algorithms. see priority_queue Class. they cannot be used with the STL algorithms. The last element pushed on the stack is the first element popped.