0% found this document useful (0 votes)
2 views24 pages

Week 16 Lec 02

The document provides an overview of the Standard Template Library (STL) in C++, covering its components such as containers, iterators, algorithms, and function objects. It details various types of containers including sequential and associative containers, along with their functionalities and examples. Additionally, it explains the use of iterators for accessing container elements and highlights algorithms for data manipulation like sorting and merging.

Uploaded by

hussaincheema715
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Week 16 Lec 02

The document provides an overview of the Standard Template Library (STL) in C++, covering its components such as containers, iterators, algorithms, and function objects. It details various types of containers including sequential and associative containers, along with their functionalities and examples. Additionally, it explains the use of iterators for accessing container elements and highlights algorithms for data manipulation like sorting and merging.

Uploaded by

hussaincheema715
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS1004 – Object Oriented

Programming
Lecture # 28
Wednesday, June 01, 2022
Spring 2022
FAST – NUCES, Faisalabad Campus

Rizwan Ul Haq
2 Standard Template Library (STL)
Introduction to STL
Containers
Iterators
Algorithms
Function Objects

CS1004 - Objected Oriented Programming


3 Introduction to STL
Powerful, template-based components
Containers: Template data structures
Iterators: Like pointers access items of containers
Algorithms: Data manipulation, searching, sorting etc.
Object-oriented programming: reuse, reuse, reuse

CS1004 - Objected Oriented Programming


4 STL Components overview
 Data storage, data access container container
and algorithms are
separated
 Containers Hold data
 Iterators access data
Algotihm
 Algorithms, function objects
manipulate data
 Allocators… allocate data
(mostly, we ignore them)
Container

CS1004 - Objected Oriented Programming


5 Container
A container is a way in which data is organized in the
memory, for example an array of elements

Container

Sequential Associative Container adapter

CS1004 - Objected Oriented Programming


6 Container
 Sequence contain
 vector
 deque
 list
 Associative containers
 set
 multiset
 map
 multimap
 Container adapter
 stack
 queue

CS1004 - Objected Oriented Programming


7 Sequential container
 vector<T> - dynamic array
 Offers random access, back insertion
 Should be your default choice, but choose wisely
 Backward compatibility with C: &v[0] points to first element
 deque<T> - double-ended queue ( usually array of arrays)
 Offers random access, back and front insertion
 Slower than vectors, not C-compatibility
 list<T>-’traditional’ doubly linked list
 Don’t expect random access, you can insert anywhere though

CS1004 - Objected Oriented Programming


8 Some functions of vector class
 size()
 Provides the number of elements
 push_back()
 Appends an element to the end
 pop_back()
 Erases the last element
 begin()
 Provide reference to first element
 end()
 Provides reference to end of vector

CS1004 - Objected Oriented Programming


9 Vector container
int arr[5] = { 11, 21, 5, 6, 15 };
vector<int> v(arr,arr+5); 11 21 5 6 15

v.pop_back(); v.push_back(20); …
15
11 21 5 6 11 21 5 6 20

11 21 5 6 20

v.Begin() v[3]

CS1004 - Objected Oriented Programming


10 Some functions of list class
function for list object t
t.sort();
Sorts in ascending order
t.splice(iterator, otherObject);
Inserts values from otherObject before iterator
t.merge(otherObject);
Merges two sorted lists. otherObject becomes empty after this
t.unique()
Removes duplicate elements

CS1004 - Objected Oriented Programming


11 Some functions of list class
list function for object t
t.swap(otherObject)
Exchange contents
t.assign(iterator1,iterator2)
replaces contents of list with values between iterator1 and iterator2
t.remove(value)
Erases all instances of value

CS1004 - Objected Oriented Programming


12 List container
int arr[5] = { 11,21,5,6,15 };
list<int> li(arr,arr+5); 11 21 5 6 15

li.pop_back(); 15 li.push_back(20); …
11 21 5 6
11 21 5 6 20
11 …
li.pop_front();
li.push_front(9);
21 5 6 20
9 21 5 6 20

CS1004 - Objected Oriented Programming


13 Function of deque class
deque functions for object d
d.front()
Returns a reference (or const_reference) to the first component of
deque
d.back()
Returns a reference (or const_reference) to the last component of
deque
d.size()
Returns a value of size_type giving the number of values currently in d

CS1004 - Objected Oriented Programming


14 Functions of deque class
d.push_back(val)
Adds val to the end of d
d.push_front(val)
Adds val to the front of d
d.pop_back()
Deletes the last value of d
d.pop_front()
Deletes the first value of d

CS1004 - Objected Oriented Programming


15 Associative containers
 Offer O(log n) insertion, suppression and access
 Stores only weakly strict ordered types (e.g. numeric types)
 Must have operator <() and operator ==() defined and !(a<b) &&
!(b<a) ≡ (a==b)
 the sorting criterion is also a template parameter
 set<T> - The items stores act as key, no duplicates
 multiset<T> - Set allowing duplicate items
 map<T,V> - Separate key and value, no duplicates
 multimap<T,V> - map allowing duplicate keys
 hashed associative containers may be available
CS1004 - Objected Oriented Programming
16 Container adaptors
Container adaptors
stack, queue, priority_queue
Not first class containers
Do not support iterators
Do not provide actual data structure
Programmer can select implementation
Member function push() and pop()

CS1004 - Objected Oriented Programming


17 Iterators
Iterator are pointer like entities that are used to access
individual elements in a container
Often these are moved sequentially from element to
element, a process called iterating through a
container.
array_ 17 vector<int>::iterator
4

23

12
The iterator corresponding to
the class vector<int> is of the
size_ 4 type vector<int>::iterator
CS1004 - Objected Oriented Programming
18 Iterator
The member function begin() and end() return and
iterator to the first and last elements of a container
respectively
v.begin()
array_ 17

23 v.end()
12

size_ 4

CS1004 - Objected Oriented Programming


19 Iterator categories
Not every iterator can be used with every container.
For example, list class provide no random access
iterator
Every algorithm require an iterator with a certain level
of capability for example to use the [] operator you
need a random access iterator
Iterators are divided into five categories, in which a
higher (more specific) category always subsumes a
lower (more general) category. E.g. an algorithm that
accepts a forward iterator will also work with a
bidirectional iterator and a random access iterator
CS1004 - Objected Oriented Programming
20 Algorithm
Algorithm in the STL are procedures that are applied
to containers to process their data, for example
search for an element in an array, or sort an array

CS1004 - Objected Oriented Programming


21 find() Algorithm
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int arr[7] = { 11,21,5,6,15, 1, 13 }; //Standard C array
int key;
vector<int> v(arr, arr + 7); //Vector v initialized with array
vector<int>::iterator iter; //Iterator in the array
cout << "Enter value: ";
cin >> key;
iter = find(v.begin(), v.end(), key); //Search key in v
if (iter != v.end()) //Found the element
cout << "Element " << key << " Found" <<endl;
else
cout << "Element " << key << " not Found" << endl;
return 0;
}

CS1004 - Objected Oriented Programming


22 Sort and Merge
 Sort and merge allow us to sort and merge elements in a container
#include <algorithm>
#include <list>
using namespace std;
int main() {
int arr1[] = { 11, 21, 15, 1, 13 }; //Stadard C array
int arr2[] = { 1, 10, 3, 12 }; //Stadard C array
int key;
list<int> l1(arr1, arr1 + 5);
list<int> l2(arr2, arr2 + 4);
l1.sort();
cout << "List 1 sorted : ";
for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
cout << *i << ' ‘;
cout << endl;
l2.sort();
cout << "List 2 sorted : ";
for(list<int>::iterator i = l2.begin(); i != l2.end(); ++i)
cout << *i << ' ‘;
cout << endl;
l1.merge(l2);
cout << "Merged List 1: ";
for (list<int>::iterator i = l1.begin(); i != l1.end(); ++i)
cout << *i << ' ‘;
cout << endl;
return 0;
}
23 Function Objects
 Some functions like sort, merge, accumulate can take a function
object as argument
 A function object is an object of a template class that has single
member function: the overloaded operator ()
 It is also possible to use user-written functions in place of pre-defined
function objects
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;
int main(){
int arr1[] = { 11, 21, 15, 1, 13 }; //Stadard C array
list<int> l1(arr1, arr1 + 5);
l1.sort(greater<int>());
return 0;
}

CS1004 - Objected Oriented Programming


24 Questions

CS1004 - Objected Oriented Programming

You might also like