Professional Documents
Culture Documents
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.
explicit vector (size_type num, const T &val=T(), const Allocator &a = Allocator());
// constructs vector that has num elements with the value val.
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:
Several member functions are defined by vector. The most commonly used are:
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.
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
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 ->.
explicit list (size_type num, const T &val=T(), const Allocator &a = Allocator());
// constructs list that has num elements with the value val.
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
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.
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: