Sequential Containers and Their Iterators in STL
Sequential Containers and Their Iterators in STL
Sequential
containers and their
iterators in STL
1/18
3. Sequential containers and their
iterators in STL
They are used for data structures that store objects of the same type
in a linear manner
2/18
3. Sequential containers and their
iterators in STL
Vector
Deque
List
Forward List
Choosing one or another will basically depend on the speed we want to have when selecting data.
This is due to the fact that each of these containers implement different algorithms for data storage.
3/18
3. Sequential containers and their
iterators in STL
4/18
3. Sequential containers and their
iterators in STL
Implements an array with fast random access and an ability to automatically resize when
adding elements
It is also known as a dynamic contiguous array (dynamic because the size of it can be changed,
in opposition to a static array)
Good to use when we do not know the upper bound number of elements we have to store (it
avoids exceptions or termination of the program)
They have dynamic size; their size can change during time
To add an element you can use push_back() function and to remove you can use pop_back()
5/18
3. Sequential containers and their
iterators in STL
6/18
3. Sequential containers and their
iterators in STL
7/18
3. Sequential containers and their
iterators in STL
Elements can be added or removed from the front (head) or the back (tail) of the queue It
allows for fast insertion and deletion of the elements at both ends
Similar to the vector container, but it is cheaper than it when expanding the size of the
storage of elements (cheaper ≡ less complex)
8/18
3. Sequential containers and their
iterators in STL
1.#include <iostream>
2.#include <deque>
3.#include <iterator>
4.using namespace std;
5.int main()
6.{
7. deque<int> d = {1, 2, 3, 4, 5};
8. d.push_front(0);
9. d.push_back(6);
10.
11. cout << "Deque: ";
12. deque<int> :: iterator itr;
13. for (itr = d.begin(); itr != d.end(); itr++)
14. {
15. cout << *itr << " ";
16. }
17. return 0;
18.}
9/18
3. Sequential containers and their
iterators in STL
More efficient when inserting/deleting elements in the middle of the sequence than
vector or array because we do not have to shift all the elements when inserting new
elements (they do not store data contiguously like vector or arrays do)
Its nodes contain 3
fields: variable of
some data type, link to
Example of a doubly-linked list: next node, link to
previous node
10/18
Example
11
3. Sequential containers and their
iterators in STL
Similar to a List, but it implements a singly linked list instead of a doubly linked list
It supports fast insertion and deletion of elements from anywhere in the container
Compared to a List (std::list) this type of container provides more space efficient storage
when bidirectional iterations are not needed
12/18
3. Sequential containers and their
iterators in STL
• The node data structure will have two fields: a variable of some data type, and the link to
the next node
• Inserting a new node on a singly-linked list can be seen below: 2 new links (between the
new node and the 2 old nodes are created) 2 new links created
13/18
3. Sequential containers and their
iterators in STL
Syntax:
forward_list<data_type> name;
Example:
14/18
3. Sequential containers and their
iterators in STL
15/18
3. Sequential containers and their
iterators in STL
16/18
3. Sequential containers and their
iterators in STL
17/18
3. Sequential containers and their
iterators in STL
Thank you!
18/18