Standard Template Library (STL) - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Standard Template Library (STL)

Description:

abstract data types (ADT) generic algorithms. Implemented as class ... Example: // Displays element of list L. list int ::iterator il; for ( il=L.begin() ; il ! ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 20
Provided by: YngviBj6
Category:

less

Transcript and Presenter's Notes

Title: Standard Template Library (STL)


1
Standard Template Library (STL)
  • Overview Part 1
  • Yngvi Bjornsson

2
STL
  • Collection of useful
  • abstract data types (ADT)
  • generic algorithms
  • Implemented as class / function templates
  • STL is
  • not a part of the C language itself
  • but a part of the C standard!

3
STL Components
  • Six main components
  • Containers
  • Sequence
  • Associative
  • Iterators
  • Constant/Mutable
  • Forward/Bi-directional/Random Access
  • Adaptors
  • Container / Interator / Function
  • Allocators
  • Generic algorithms
  • Function objects

4
Containers
  • Sequence Containers
  • Arrange data-items into a linear arrangement.
  • Container types
  • vectorltTgt
  • dequeltTgt
  • listltTgt
  • (Sorted) Associative Containers
  • Fast insert/retrieval of data-items based on keys
  • Container types
  • setltKeygt multisetltKeygt
  • mapltKey,Tgt multimapltKey,Tgt
  • "multi" containers allow multiple items with same
    key.

5
Sequence Containers
  • Vector
  • Efficient add/delete back, access/modify any
    element
  • Deque
  • Efficient add/delete front/back, access/modify
    any element
  • List
  • Efficient add/del. elements anywhere, access/mod.
    front/back

5
6
6
5
6
6
5
6
6
6
Common Operations on Seq. Containers
Descr. Method V D L Descr. Method V D L
E front (ref) front Del all clear
E back (ref) back Add pos insert ? ?
Add E back push_back Del pos erase ? ?
Del E back pop_back lt
Add E front push_front - !
Del E front pop_front -
E at subscript - E size
7
include ltlistgt include ltvectorgt include
ltdequegt using namespace std int main() //
STL Vector can use as an array (grows
dynamically!) vectorltintgt V1(10), V2 for (
int i0 ilt10 i ) V1i i //
NOTE cannot use subscript unless element
exists! V2.push_back(i) for ( int
i0 ilt10 i ) cout ltlt V1i ltlt " " ltlt V2i
ltlt endl // STL Deque dequeltintgt D1, D2
for ( int i0 ilt10 i ) D1i i
D2.push_front(i) for ( int i0 ilt10 i
) cout ltlt D1i ltlt " " ltlt D2i ltlt endl //
STL List (no subscripting) listltintgt L1, L2
for ( int i0 ilt10 i )
L1.push_back(i) L2.push_front(i) for (
int i0 ilt10 i ) cout ltlt L1.front()
ltlt " " ltlt L2.back() ltlt endl
L1.pop_front() L2.pop_back()
8
Iterators
  • Standard interface of how to traverse elements in
    a container.
  • Allows us to write generic algorithms that work
    for any container! ( almost)
  • Iterators behave in a way like pointers
  • (dereference), , --, , !, , -
  • Can think of as generalized pointers

9
Iterators and Containers
  • The container classes have methods
  • begin() Returns an iterator "pointing" to first
    element
  • end() Returns an iterator pointing after last
    element
  • (also rbegin() and rend() for reverse traversal)
  • Example
  • // Displays element of list Llistltintgtiterator
    ilfor ( ilL.begin() il ! L.end() il )
    cout ltlt il ltlt endl

10
Iterator Kind
  • Kind
  • Forward iterators
  • , !, , (dereference)
  • Bi-directional iterators
  • -- (additially to forward iterators)
  • Random access iterators
  • , , -, , - (additionally to bi-directional
    iterators)
  • Containers
  • List (bi-directional), vector and deque
    (random-access)

11
Iterator Declaration
  • Mutable (default)
  • listltintgtiterator
  • Constant
  • Listltintgtconst_iterator
  • Reverse (also mutable)
  • listltintgtreverse_iterator
  • Constant and Reverse
  • listltintgtconst_reverse_iterator

12
Generic Algorithm
  • templatelttypename IterType, typename ElemTypegt
  • IterType find(IterType first, IterType last,
    const elemType value)
  • for ( first ! last first)
  • if (value first)
  • return first
  • return last
  • vectorltintgt ivec
  • vectorltintgtiterator it find(ivec.begin(),
    ivec.end(), 1024)
  • if (it!ivec.end()) // found 1024 in ivec
  • listltstringgt slist
  • listltintgtiterator iter find(slist.begin(),
    slist.end(), "duck")
  • if (iter!slist.end()) // found "duck in
    slist

13
Container Adaptors
  • Container classes implemented on top of the
    existing containers (adapt their interface)
  • stack (default container deque)
  • queue (default container deque)
  • priority_queue (default container vector)
  • Can change underlying container class
  • stackltint, vectorltintgt gt
  • Note, need space in between gt

Why?
14
Operations on Adapter Containers
Descr. Method S Q P
of elements (E) size
is empty? empty
E at top (ref) top -
Add E top / back push
Del E top / front pop
E at front (ref) front - -
E at back (ref) back - -
15
include ltstackgt include ltqueuegt using namespace
std int main() // STL Stack stackltint,
vectorltintgt gt S // Changing default container
(note 'gt gt', not 'gtgt') for ( int i0 ilt10
i ) S.push(i) for ( int i0 ilt10 i )
cout ltlt S.top() ltlt " " S.top() 2
S.top() cout ltlt S.top() ltlt endl
S.pop() // STL Queue queueltintgt Q
for ( int i0 ilt10 i ) Q.push(i) for (
int i0 ilt10 i ) cout ltlt Q.front() ltlt
endl Q.pop() // STL Priority Queue
priority_queueltintgt P for ( int i0 ilt10
i ) P.push(i) for ( int i0 ilt10 i )
cout ltlt P.top() ltlt endl P.pop()
16
Associative Containers
  • set
  • Add/Delete elements to/from set (no duplications)
  • Can ask of membership (is in set)
  • map (associative array)
  • Add/Delete pairs to/from map (no duplications of
    keys)
  • multiset / multimap essentially the same except
    duplications of keys allowed.

a
b
c
d
a
1
b
2
c
1
d
0
17
Common Operations on Associative Containers
Descr. Method S M
of elements (E) size
is empty? empty
Add element insert
Delete element erase
Find element (ref) find
The same
Subscript with key
18
include ltstringgt include ltsetgt include
ltmapgt using namespace std int main() // STL
set setltstringgt S S.insert("hi")
S.insert("how") S.insert("are")
S.insert("you") for ( setltstringgtiterator
isS.begin() is ! S.end() is ) cout
ltlt is ltlt endl // STL map
mapltstring,intgt M M"Hi" 0 M"how"
1 M"are" 2 M"you" 1 if (
M.find("how") ! M.end() ) cout ltlt "'how'
is in map" ltlt endl for ( mapltstring,intgtitera
tor imM.begin() im!M.end() im )
cout ltlt im-gtfirst ltlt " " ltlt im-gtsecond ltlt endl
// note use of pair
19
Summary
  • Overview of STL
  • Containers
  • Iterators
  • Some of the containers have
  • additional methods not covered here
  • e.g. can splice lists
  • For complete documentation (and intro)
  • http//www.sgi.com/tech/stl/
Write a Comment
User Comments (0)
About PowerShow.com