0% found this document useful (0 votes)
12 views18 pages

Sequential Containers and Their Iterators in STL

The document provides an overview of sequential containers in the C++ Standard Template Library (STL), including types such as Vector, Deque, List, and Forward List, each with distinct characteristics and use cases. It explains the functionality of iterators for these containers and highlights the performance implications of different operations. Additionally, it discusses the running time complexity of various operations associated with these containers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

Sequential Containers and Their Iterators in STL

The document provides an overview of sequential containers in the C++ Standard Template Library (STL), including types such as Vector, Deque, List, and Forward List, each with distinct characteristics and use cases. It explains the functionality of iterators for these containers and highlights the performance implications of different operations. Additionally, it discusses the running time complexity of various operations associated with these containers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

3.

Sequential
containers and their
iterators in STL

1/18
3. Sequential containers and their
iterators in STL

What is a sequential container?

 Container class template in the standard library of C++ that


implements storage of data elements, in a sequential or ordered way

 It is a type of container in the STL in C++

 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

Types of sequential containers:

 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

Iterators – Sequential containers

Any object that points to an element. Most obvious iterator: a pointer;


points to elements in an array, and iterates through them using sets of
operators.

4/18
3. Sequential containers and their
iterators in STL

Vector – Sequential container

 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

Vector – Example Implementation of Push Back and traversal using iterators

Push_Back() can be used to add


elements to the vector

6/18
3. Sequential containers and their
iterators in STL

Vector – Example Implementation of Pop Back and traversal using iterators

Pop_Back() can be used to


remove elements from the vector

7/18
3. Sequential containers and their
iterators in STL

Deque – Sequential container

 Implements a double-ended queue with reasonable fast random access

 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

Deque – Example Implementation and traversal using iterators

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

List – Sequential container and


implementation

 Implements a doubly-linked list: linked data structure that consists of a set of


sequentially linked records (data structures) called nodes

 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

Forward List – Sequential container

 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

Forward List – Sequential container

Example of a singly-linked list:

• 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

Forward List – Example Implementation

Syntax:
forward_list<data_type> name;

data_type: data type of the elements to be stored in the list.

Example:

forward_list<int> l; //initializes a forward list of size 0 which stores integer values

14/18
3. Sequential containers and their
iterators in STL

Iterators – Sequential containers seen

15/18
3. Sequential containers and their
iterators in STL

Running time complexity of operations

16/18
3. Sequential containers and their
iterators in STL

Running time complexity of operations

O(n) = Running time


complexity is proportional
to the amount of data (n)
the algorithm is working
with

O(1) = No matter the


amount of data, the
algorithm will be executed
at constant time

17/18
3. Sequential containers and their
iterators in STL

Thank you!

18/18

You might also like