You are on page 1of 4

What are Vectors and Deques?

Vector:

Vector is one type of standard container provided by C++ library. The vector class
supports dynamic array ie it can grow as needed. It supports all the functions of an array
(ie accessing array elements using subscript notation) at the same time provides the
flexibility of changing size of array at runtime to accommodate changing program
conditions.

Template specification for a vector looks like:


template <class T, class Allocator = allocator <T>> class vector

Vector has following constructors:

Explicit vector (const Allocator &a = Allocator()); // Constructs empty vector

explicit vector (size_type num, const T &val=T(), const Allocator &a = Allocator());
// constructs vector that has num elements with the value val.

vector (const vector &ob);


// constructs a vector that has same elements as ob

template <class InIter> vector(InIter start, InIter end, const Allocator &a = Allocator());
// constructs a vector that contains the elements in the range specified by the iterators start
and end.

Examples:

vector <int> iv; //create zero length int vector


vector <char> cv(5); //create 5 element char vector
vector <char> cv(5, ‘x’); //initializes a 5-element char vector
vector <int> iv; //create a vector from an int vector

Several member functions are defined by vector. The most commonly used are:

size() – returns size of vector


begin() – returns iterator to the start of vector
end() - returns iterator to the end of vector
push_back() – puts value to the end of vector (dynamically increase size of vector)
insert()- adds elements to the middle
erase()- removes elements from vector

Deque:
It’s a double ended queue. That is, it’s a sequence optimized so that operations at both
ends are efficient. Its template specification is:
template <class T, class Allocator = allocator <T>> class deque
Here, T is the type of data stored in the deque.

It has the following constructors:

explicit deque (const Allocator &a = Allocator()); // Constructs empty deque


explicit deque (size_type num, const T &val=T(), const Allocator &a = Allocator());
// constructs deque that has num elements with the value val.
deque (const deque <T, Allocator> &ob);
// constructs a deque that has same elements as ob

template<class InIter> deque(InIter start, InIter end, const Allocator &a = Allocator());
// constructs a deque that contains the elements in the range specified by the iterators start
and end.

Examples:
deque <int> iv; //create zero length int deque
deque <char> cv(5); //create 5 element char deque
deque <char> cv(5, ‘x’); //initializes a 5-element char deque
deque <int> iv; //create a vector from an int deque
Vector vs. deques

Vector supports dynamically expandable array.


Deque is double ended queue. Deque elements can be accessed from both the ends while
vector elements can be accessed using subscript notion.

Explain Iterators with examples.


Iterators are the glue that hold the container and algorithms together. They provide an
abstract view of data so that the writer of algorithm need not be concerned with concrete
details of a myriad of data structures. At the same time, the standard model of data access
provided by iterators relieves the containers from having to provide a more extensive set
of access operations.

An iterator is simply an abstract of the notion of a pointer to an element of a sequence.

The key concepts of iterator are:

- The element currently pointed to


- Point to next element
- Equality

The iterator classes and functions are declared in namespace std and found in <iterator>.

The iterator is not a general pointer. Hence there is no concept of ‘null iterator’. The test
is to determine whether an iterator points to an element or not is done by comparing it
against the end of its sequence. An iterator which points to an element is said to be valid
and can be dereferenced using *, [] or ->.

What are Lists? Explain with an example.


List class supports a bidirectional linear list. Unlike a vector, which supports random
access, a list can be accessed sequentially only. Since lists are bidrectional, they may be
accessed front to back or back to front. The template specification of list is:

template <class T, class Allocator = allocator<T>> class list

Here, T is the type of data stored in the list.

It has the following constructors:

explicit list (const Allocator &a = Allocator()); // Constructs empty list

explicit list (size_type num, const T &val=T(), const Allocator &a = Allocator());
// constructs list that has num elements with the value val.

list (const list <T, Allocator> &ob);


// constructs a list that has same elements as ob

template <class InIter> list(InIter start, InIter end, const Allocator &a = Allocator());
// constructs a list that contains the elements in the range specified by the
iterators start and end.

Examples:
list <int> iv; //create zero length int list
list <char> cv(5); //create 5 element char list
list <char> cv(5, ‘x’); //initializes a 5-element char list
list <int> iv; //create a vector from an int list

Some of the commonly used list functions are:

push_back(): to put elements to the list


push_front(): to put elements to the front of the list
insert(): to put elements in the middle of the list
Two lists can be joined using splice(). One list can be merged into another using merge()

Describe associate containers.


Containers are objects that hold other objects. An associative container stores pair of
values. It is typically a key-value pair. Given one value (key), we can access the other,
called the mapped value. The key needs to be unique. The value associated with that key
could be unique or multiple depending upon the type of associative container.

The key-value pair could be of any data type (unlike integer in case of array).
There are various types of associative containers:

Map: It is a traditional associate array, where a single value is associated with each
unique pair.

Multimap: This type of associative array allows duplicate elements (value) for a given
key.

What is Stream Iterator?


Iterators are the glue that hold the container and algorithms together. They provide an
abstract view of data so that the writer of algorithm need not be concerned with concrete
details of a myriad of data structures. At the same time, the standard model of data access
provided by iterators relieves the containers from having to provide a more extensive set
of access operations.

The iterators provided by Standard Library for stream I/O are known as stream iterators.
The basic idea behind is simply to present input and output of collections as sequences.

For input and output there are different stream iterators provided:

ostream_iterator: to write to an ostream


istream_iterator: to read from an istream
ostreambuf_iterator: to write to a stream buffer
istreambuf_iterator: to read from a stream buffer

You might also like