CS304: Lecture 12 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS304: Lecture 12

Description:

Modern programming language philosophy centers around software reuse ... Associative. Set. Multiset. Map. Multimap. Adapters. Stack. Queue. Priority Queue ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 11
Provided by: matthe49
Category:

less

Transcript and Presenter's Notes

Title: CS304: Lecture 12


1
CS304 Lecture 12
  • The Standard Template Library
  • Deitel, Ch. 23
  • http//www.cis.uab.edu/cs304

2
Why the STL?
  • Modern programming language philosophy centers
    around software reuse
  • With the STL, C not only encourages reusable
    software, but also provides reusable software
  • The STL, through use of templates, gives the user
    a set of data structures, algorithms, and
    iterators to create type-safe, fool-proof
    containers.

3
The Data Structures
  • Examples in this category include many of the
    structures you are familiar with
  • Vector (dynamic array)
  • List (linked list)
  • Deque (Double Ended Queue)
  • Share common functionality

4
Iterators
  • Iterators are glorified, safe pointers
  • Type Safety
  • Bounds safety
  • Yet, arithmetic is still allowed!
  • Convenient and expressive when combined with
    algorithms
  • Many types, depending on your needs!

5
Algorithms
  • Examples of some algorithms available
  • Sort
  • Reverse
  • Copy
  • Remove
  • Unique

6
Sequence Containers -- Vector
  • An array-based container
  • Dynamically resizes, assignable, optional bounds
    checking (using at)
  • Includes functions for insertion at any position
    of the vector
  • Due to the array-ness of the vector, insertion is
    efficient only at the end

7
Sample Vector Functions
  • Front Returns an iterator that points to the
    first element of the vector
  • Back Returns an iterator that points to the
    last element of the vector
  • Reference an element within a vector
  • Push_back Insert an element at the end of the
    vector
  • Size Returns the size of a vector

8
Iterator Example on a Vector
  • include ltvectorgt
  • include ltiostreamgt
  • using namespace std
  • int main()
  • vectorltintgt v
  • vectorltintgtconst_iterator cIter
  • for (int i 0 i lt 100 i)
  • v.push_back(i)
  • for (cIter v.begin() cIter ! v.end()
    cIter)
  • cout ltlt cIter ltlt ' '

9
Using an algorithm on a vector
  • include ltcstdlibgt
  • include ltvectorgt
  • include ltiostreamgt
  • using namespace std
  • int main()
  • vectorltintgt v
  • for (int i 0 i lt 100 i)
  • v.push_back(rand())
  • sort(v.begin(), v.end())
  • for (int i 0 i lt v.size() i)
  • cout ltlt vi ltlt endl

10
Other Types of Containers
  • Associative
  • Set
  • Multiset
  • Map
  • Multimap
  • Adapters
  • Stack
  • Queue
  • Priority Queue
Write a Comment
User Comments (0)
About PowerShow.com