Department of Computer and Information Science, School of Science, IUPUI - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Department of Computer and Information Science, School of Science, IUPUI

Description:

There are four types of associative containers - set, multiset, map, multimap ... The keys in multiset and multimap are equal keys. The following header files ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 17
Provided by: dalero
Learn more at: http://cs.iupui.edu
Category:

less

Transcript and Presenter's Notes

Title: Department of Computer and Information Science, School of Science, IUPUI


1
Department of Computer and Information
Science,School of Science, IUPUI
Standard Template Library
  • Dale Roberts, Lecturer
  • Computer Science, IUPUI
  • E-mail droberts_at_cs.iupui.edu

2
Introduction
  • Industrial revolution - Reuse software, not
    re-write
  • Two main paradigms that permit code reuse -
    Object oriented design, Generic Programming
  • OOD facilitates code reuse by Encapsulation,
    Inheritance and Polymorphism
  • Generic programming facilitates code reuse by
    data independence - Templates and Overloading
  • STL is a combination of both OOD and Generic
    programming - collection of generic algorithms
    and containers that communicate through
    iterators. 

3
Need for STL
  • Reusability - adaptability vs efficiency
  • Highly adaptive components are usually
    implemented using complex inheritance and virtual
    functions too inefficient to be widely used
  • Highly efficient components - written in low
    level,  platform dependent code - non-portable,
    hard to maintain
  • Solution- Templates - containers and algorithms
  • Address the drawbacks of writing your own code -
    not portable, less than 100 bug free and no
    common interface.

4
Structure of STL
  • The Standard Template Library contains the
    following five components
  • Containers
  • Iterators
  • Algorithms
  • Function objects
  • Adaptors

5
Containers
  • Holds other object as its element
  • The allocation and deallocation of these objects
    are controlled through constructors,
    destructors,  insertion, and erase operation
  • There are two types -
  • Sequence containers - vector, queue, dequeue, and
    list
  • Associative containers - set, map, and multimap

6
Sequence Containers
  • Organizes a finite set of same type objects into
    a strictly linear arrangement
  • There are 3 basic types - vector, list and
    dequeue
  • The containers queue and stack are implemented
    using dequeue
  • The following are the header files used

7
Associative Containers
  • Facilitates fast retrieval of data based on keys
  • There are four types of associative containers -
    set, multiset, map, multimap
  • Parameterized on key and an ordering relation
    Compare
  • The keys in set and map are unique keys
  • The keys in multiset and multimap are equal keys
  • The following header files are used

8
Sequence Containers - Example 1
  • includeltiostreamgt
  • includeltvectorgt
  • includeltstringgt
  • //using namespace std
  • int main()
  • //An object of vector containing string objects
    are created
  • vector ltstringgt vs
  • //Make room for 10 strings using the reserve(n)
    function.
  • //The reserve(n) function just allocates memory
    for n elements.
  • //Thus, this changes the value returned by the
    capacity() funciton.
  • vs.reserve(10)
  • //The member function push_back(T) appends a
    single element
  • //to the end of the container. If the container
    capacity
  • //is reached, it reallocates additional storage
    and appends
  • //the element.

9
Sequence Containers - Example 1
  • //are currently stored in the container
  • cout ltlt "Size " ltlt vs.size() ltlt endl
  • //The capacity() function returns the number of
    elements that
  • //the container can hold before reallocation
  • cout ltlt "Capacity " ltlt vs.capacity() ltlt endl
  • //The difference in capacity() and size() gives
    the remaining
  • //space in the container.
  • cout ltlt "There's room for " ltlt vs.capacity() -
    vs.size()
  • ltlt " elements before reallocation" ltlt endl
  • //Allocate 10 more elements using resize(n) The
    resize(n) function,
  • //in addition to allocating memory for n
    elements, initializes them
  • //to default value. A different value can be
    provided as the second
  • //argument if needed. Thus, this changes the
    value returned by both
  • //capacity() and size()
  • vs.resize(20)

10
Sequence Containers - Example 1 (output)
  • Size 1
  • Capacity 10
  • There's room for 9 elements before reallocation
  • Size 20
  • Capacity 20

11
Sequence Containers - Example 2
  • This example illustrates the use of Front and
    Back operations.
  • includeltiostreamgt
  • includeltvectorgt
  • includeltstringgt
  • using namespace std
  • int main()
  • vector ltstringgt vs
  • vs.push_back("string 1") //insert an element
  • vs.push_back("string 2") //insert an element
  • cout ltlt "Size " ltlt vs.size() ltlt endl
  • cout ltlt "Capacity " ltlt vs.capacity() ltlt endl
  • //The member function front() accesses a single
  • //element at the front of the container
  • //

12
Algorithms
  • STL contains a rich collection of algorithms that
    can be applied to a variety of containers.
  • There are approximately 70 standard algorithms
  • Operate on elements of containers indirectly
    through iterators
  • There are three main types of algorithms.
  • Non-mutating sequence operations
  • Mutating sequence operations
  • Sorting Algorithms

13
Non-mutating sequence operations
  • These algorithms do not modify the sequence on
    which they operate.
  • They include searching, checking for equality,
    and counting, for example
  • Example -
  • find( ) - Locates an element within a sequence. 
    Takes 3 arguments, the first 2 are iterators that
    point to begin and end of sequence respectively. 
    The third element is the value to be found. 
    Returns an iterator that points to the first
    element that is identical to the value to be
    found.

14
Mutating sequence operations
  • Modifies the sequence on which they operate
  • Includes operations such as copy, fill, replace,
    and transform, for example
  • Example -
  • copy( )  - This is a generic function that can be
    used to copy a sequence of objects to specific
    target.  Takes 3 arguments.  The first 2 are the
    begin and end iterators of the object to be
    copied and the third is the target object.

15
Sorting operations
  • Algorithms for sorting and merging sequences
  • Algorithms for set operations for sorted
    sequences
  • Includes sort(), partial_sort(), binary_search(),
    for example
  • Example
  • sort( ) - takes two arguments of type const
    iterator that point to the beginning and the end
    of the sequence respectively. 

16
C STL Reference
  • Internet Resource
  • http//www.cppreference.com/cppstl.html
Write a Comment
User Comments (0)
About PowerShow.com