An Overview of the - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

An Overview of the

Description:

multimap. multiset. Algorithms. Algorithms act on Containers ... multimap. Stores key/value pairs map multiset. A set in which each element may not be unique ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 21
Provided by: bryce7
Category:

less

Transcript and Presenter's Notes

Title: An Overview of the


1
  • An Overview of the
  • Standard Template Library
  • Part 1

CSCE 221, Texas AM University Adapted from
RebootSoft http//www.rebootsoft.com/Presentations
/stl/STL.zip
2
Overview of the STL
  • What is the STL ?
  • History of the STL
  • C Templates 101
  • STL Components
  • Containers
  • Algorithms
  • Iterators
  • Allocators, Function Objects Adaptors
  • The Container Classes Hierachy
  • The Vector Class

3
What is the STL ?
  • Part of the Standard C library
  • Set of C template classes and functions
  • Implements many common
  • Data structures
  • E.g. lists and queues
  • Algorithms
  • E.g. sorting and searching
  • Can be applied to nearly any type of data
  • Developed by CS professionals over years
  • Many compilers now support it
  • MS VC6, Borland C, gcc, etc.

4
History of the STL
  • Brainchild of Alexander Stepanov
  • 1979 initial research into generic programming
  • Much of the work done when Stepanov worked at HP
  • Early work was done on C and Ada
  • 1987 produced a generic Ada library
  • However Stepanov realized that STL best suited
    C
  • 1990 the implementation language was changed to
    C
  • 1994 ISO committee voted to include STL into C
  • 1995 HP reference implementation released on the
    internet

5
C Templates 101
  • Templates are not types, but rather they are a
    placeholder for a type
  • C templates come in two flavors
  • Functions templates
  • Class templates

6
C Templates 101
  • Functions templates
  • used to define generic algorithms.
  • While this is useful, it only works for integers.
  • A better solution is to define a function
    template for max

int max(int x, int y) return x lt y ? y x
template ltclass Tgt T max(T x, T y) return x lt
y ? y x
7
C Templates 101
  • Nothing special has to be done to use a function
    template
  • Note all that is required of the type passed to
    max is the comparison operator, operatorlt.

int main(int argc, char argv) int a 3, b
7 double x 3.14, y 2.71 cout ltlt
max(a, b) ltlt endl // Instantiated with type
int cout ltlt max(x, y) ltlt endl // Instantiated
with type double cout ltlt max(a, x) ltlt endl //
ERROR types do not match
8
C Templates 101
  • Class Templates
  • May contain data member objects of any type.

template ltclass Tgt class myarray public
T v int sz myarray(int s) v new
T sz s // Constructor myarray()
delete v // Destructor T operator (int
i) return vi void set(int i, T val)
vi val int size() return sz
  • Then instantiate the same container with
    objects of different types

myarrayltintgt iwidget(10) myarrayltshapegt
swidget(10)
9
STL Components
Containers
Allocators
Adaptors
TRANSFORMS
PROVIDE STORAGE FOR
TRANSFORMS
ACCESS
APPLIED TO
Generic Algorithms
Iterators
USE
USE
ASSIST
Function Objects
TRANSFORMS
Simply put Algorithms act on Containers through
Iterators.
10
Containers
  • Containers are objects that hold other objects
  • There are 2 main types of container
  • Sequence Containers
  • vector
  • deque
  • list
  • Associative Containers
  • Each container defines function that are
    meaningful to it.
  • list container defines functions to insert
    delete
  • stack container defines functions to push and pop

11
Algorithms
  • Algorithms act on Containers
  • BUT Algorithms are decoupled from specific
    containers
  • Capabilities include
  • Initialization
  • Sorting
  • Searching
  • Many algorithms can operate on a range of
    elements within a container.

Algorithms
Iterators
Containers
12
Iterators
  • Iterators are objects that act like pointers.
  • Provide the ability to cycle through the contents
    of a container.
  • There are 5 types of iterator
  • Random access
  • Bidirectional
  • Forward
  • Input
  • Output
  • Iterators are handled liked pointers
  • i.e. You can increment and decrement them.
  • You can apply the operator to them.

Random access
(More general)
Bidirectional
Forward
(More restrictive)
Input
Output
13
Allocators, Function Objects Adaptors
  • Allocators
  • Manage memory for Containers
  • In most cases the default Allocator is sufficient
  • However its available if you wanted you do you
    own memory management
  • Function Objects
  • Function objects are classes that define
    operator()
  • Several predefined function, less(), greater(),
    plus()
  • They can be used in place of function pointers in
    STL algorithms
  • Adaptors
  • An Adaptor transforms one thing into another
  • There are Container adaptors, Iterator adaptors
    Function adaptors
  • Example of a container adaptor is queue
  • This adapts a the deque container for use a
    standard queue.

14
The Container Classes
Sequence Containers , Associative Containers ,
Container Adaptors
15
The Container Hierarchy
Collections
Containers
Adaptors
Sequence
Associative
16
Sequence Containers
  • Three types of sequence containers in STL
  • They store data in linear sequence.
  • vector
  • deque
  • list
  • To choose a container

17
The vector Container
  • One of the most commonly used containers
  • Composed of a single block of sequential memory
  • Supports a dynamic array
  • Use when objects are inserted/removed primarily
    at the end
  • Vectors template specification is
  • template ltclass T, class Allocator
    allocatorltTgt gt class vector
  • T is the type of data being stored
  • Allocator specifies the allocator

vectorltchargt cv1 // create zero-length char
vector vectorltintgt iv1(10) // create 10
element int vector vectorltchargt cv2(5, a) //
initialize a 5 element char vector vectorltintgt
iv2(iv1) // create int vector from an int vector
18
The vector Class member functions
19
Using the vector Container
Example vector1
vectorltintgt v(10) // create vector of length
10 printf(Size i\n, v.size() ) // display
original size for(i0 ilt10 i) vi i //
Assign elements values for(i0 ilt10 i)
printf(i , vi ) // display
elements for(i0 ilt5 i) // Add 5 elements
to vector v.push_back(i10) printf(New size
i\n, v.size() ) // display new size for(i0
ilt10 i) vi -vi // Change element values
OUTPUT Size 10 0 1 2 3 4 5 6 7 8 9 New size
15
20
Using the vector iterator
Example vector3
vectorltintgt v for(i0 ilt10 i)
v.push_back(i) // Assign 10 new
elements vectorltintgtiterator p
v.begin() while( p ! v.end() ) printf("i ",
p ) // Print elements forwards p p
v.end() while( p ! v.begin() ) p-- printf("
i ", p ) // Print elements backwards
Print elements forwards 0 1 2 3 4 5 6 7 8
9 Print elements backwards 9 8 7 6 5 4 3 2 1 0
Write a Comment
User Comments (0)
About PowerShow.com