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
- Point to next element
- Equality
The iterator classes and functions are declared in namespace std and found in .
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?
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 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.
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 &ob);
// constructs a list that has same elements as ob
// constructs a list that has same elements as ob
template 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.
// constructs a list that contains the elements in the range specified by the
iterators start and end.
Examples:
list iv; //create zero length int list
list cv(5); //create 5 element char list
list cv(5, ‘x’); //initializes a 5-element char list
list iv; //create a vector from an int list
list
list
list
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()
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()
What are 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:
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.

0 Comments:
Post a Comment