The STL sequence containers allow controlled sequential access to elements. Sequence containers hold data elements in a linear series, as observed in Figure 20.2. Every element is associated by its location within the series. All these elements enlarge themselves to enable the insertion of elements. These elements also allow various operations, such as copying and finding.
Fig. 20.2 Data items in sequence container.
The STL has three types of sequence containers:
Iterators are used to get the elements in all these containers. All these containers have a different speed.
We know that an array is used to store similar data elements. Elements of the array are stored in successive memory locations and are accessed in order from element number 0 onward. The vector class exactly acts similar to an array. This class is more secure and efficient than arrays.
It is used to insert or delete an element. All the elements are accessed randomly; that is, they support random access to individual elements.
A vector container class is accelerated to enable quick access to its elements in sequence. It is defined in the header file <vector>. A vector is able to enlarge itself. For example, if a vector declared for 5 elements is assigned 6 elements, then the vector automatically develops its size so that it can hold the 6th element. The vector class is declared in the following manner:
Declaration of Vector class
template <class TE>, class B =allocator <TE> class vector
Here, the class TE is the type of element in the vector; the class B is an allocator class. M_allocators are responsible for the allocation and release of memory. By default, the new() and delete() operators are also used for the allocation and release of memory. The default constructor of the class TE is invoked to produce a new element. This allows the entry of another parameter in order to define a default constructor for the user defined classes. The vectors that hold integers and floats are declared as follows:
Vector declaration for int and float data
vector<int> vi // for integer elements
vector <float> vf // for float elements
To declare a vector of 15 items, a constructor can be declared as follows:
The compiler allocates memory for 15 elements. Here, the default constructor item :: item() is used.
The list container enables the programmer to perform the usual deletion and insertion of items. A list is also a sequence that can be accessed bi-directionally. It is defined in header file <list>. It acts as a double-linked list. Every node has a connection to both back and front nodes in the list. The iterator is used to transverse the link. The list class supports all the member functions of the vector class. The elements in the linked list are accessed using pointers. The list container has a technique known as an iterator, which is used to access the elements of the list container. An iterator is similar to a pointer. The iterator can be referenced similar to pointers to access elements.
A deque is similar to a multiple-ended vector. Similar to the vector class, it has the ability to perform sequential read and write operations. The deque class allows improved front-end and back-end operations. Deques are perfectly appropriate for the operation that contains insertion and deletion at one or both ends and where sequential access is essential.