Standard Template Library STL in C
Standard Template Library STL in C
in C++
CSE 1018Y Computer Programming
Introduction
STL defines powerful, template-based
reusable components that implement many
common data structures and algorithms used
to process those data structures.
The three key components of STL are
containers, iterators and algorithms.
Introduction cont..
STL containers are data structures capable of
storing objects of any data type.
Each STL container has associated member
functions.
A subset of these member functions is defined
in all STL containers
Example: STL container vector
Introduction cont..
STL iterators have similar properties as those
of pointers.
It is used by programs to manipulate the STLcontainer elements.
Reduce many lines of code to a single
statement.
Introduction cont..
STL algorithms are functions that perform
searching , sorting and comparing elements
(entire containers).
Most of the STL algorithms use iterators to
access container elements
Each algorithm has minimum requirements for
the type of iterators that can be used with it
Containers
Containers are divided into 3 major
categories: sequence container, associative
container and container adapters
Sequence Containers
The sequence containers represent linear data
structures such as vectors and linkedlist
Standard Library Sequence Container Class
are:
vector (rapid insertions and deletions at back
direct access to any elements)
deque (rapid insertions and deletions at front or
back direct access to any elements)
List (rapid insertions and deletions anywhere)
Associative Containers
Assocaitve Containers are non-linear containers
that typically can locate elements stored in the
containers quickly
Store set of values or key/value pairs
Standary Library Associative Container Class are:
Set(Rapid lookup, no duplicates allowed)
Multiset (Rapid lookup, duplicates allowed)
Map (one-to-one mapping, no duplicates allowed,
rapid key-based lookup)
Multimap (one-to-one mapping, duplicates allowed,
rapid key-based lookup)
Container Adapters
Standard Library Container Adapters Class are:
Stack (last in, first-out LIFO)
Queue (first-in, first-out FIFO)
Priority Queue highest priority element is always
the first element out
<vector>
<list>
<deque>
<queue> contains both queue and priority queue
<stack>
<map> contains both map and multimap
<set>
contains both set and multiset
<bitset>
Vector
Container
int array[5] = {12, 7, 9, 21, 13 };
vector<int> v(array,array+5);
12
21
13
v.push_back(15);
v.pop_back();
12
21
12
12
21
15
v.begin();
v[3]
21
15
Vector Container
#include <vector>
#include <iostream>
vector<int> v(3); // create a vector of ints of size 3
v[0]=23;
v[1]=12;
v[2]=9; // vector full
v.push_back(17); // put a new value at the end of array
for (int i=0; i<v.size(); i++) // member function size() of
vector
cout << v[i] << ; // random access to i-th element
cout << endl;
STL Iterators
Iterators are often used to move sequentially from element to
element, a process called iterating through a container.
Used as arguments to algorithms
The iterators category that each container supports determines
whether the container can be used with all algorithms in the STL
Containers provide several iterators
Begin, End, Reverse Begin, Reverse End
STL iterators
5 categories each has specific operations
Input
Output
Forward
Backward
Random access
, No assignment of *i
Output Operations: =, *, ++
Forward Operations: =, ==, !=, *, ->, ++
Bidirectional Operations: =, ==, !=, *, ->, ++, -Random Operations: =, ==, !=, +=, -=, *, ->, +, ++, -, --, [n], <, <=, >, >=
Iterators
#include <vector>
#include <iostream>
int arr[] = { 12, 3, 17, 8 };
vector<int> v(arr, arr+4); // initialize vector
vector<int>::iterator iter=v.begin(); // iterator for class vector
// define iterator for vector and point it to first element of v
cout << first element of v= << *iter; // de-reference iter
iter++; // move iterator to next element
iter=v.end()-1; // move iterator to last element
Iterators
int max(vector<int>::iterator start, vector<int>::iterator
end)
{
int m=*start;
while(start != stop)
{
if (*start > m)
m=*start;
++start;
}
return m;
}
cout << max of v = << max(v.begin(),v.end());
STL Algorithms
Accepts STL iterators as arguments
sort(begin,end);
4 categories
Non-modifying
For each, Find, Count, Equal, Search, etc.
Mutating
Copy, Generate, Reverse, Remove, Swap, etc.
Sorting Related
Sort, Stable sort, Binary search, Merge, etc.
Numeric
Accumulate, Inner product, Partial sum,
Adjacent difference
For_Each() Algorithm
#include <vector>
#include <algorithm>
#include <iostream>
void show(int n)
{
cout << n << ;
}
Find() Algorithm
#include <vector>
#include <algorithm>
#include <iostream>
int key;
int arr[] = { 12, 3, 17, 8, 34, 56, 9 };
vector<int> v(arr, arr+7); // initialize vector
vector<int>::iterator iter;
cout << enter value :;
cin >> key;
iter=find(v.begin(),v.end(),key); // finds integer key in v
if (iter != v.end()) // found the element
cout << Element << key << found << endl;
else
cout << Element << key << not in vector v << endl;
References
C++ How to program, Fifth Edition, Deitel &
Deitel