CS 403 Programming Languages - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

CS 403 Programming Languages

Description:

Quick (Re)Introduction to C Templates. Overview of the Standard ... iota, accumulate, inner_product, partial_sum, adjacent_difference, power. CS 403, Class 23 ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: joelj
Category:

less

Transcript and Presenter's Notes

Title: CS 403 Programming Languages


1
CS 403 - Programming Languages
  • Class 23
  • November 16, 2000

2
Todays Agenda
  • Augment Chapter 10
  • Quick (Re)Introduction to C Templates
  • Overview of the Standard Template Library
  • Example Code deque stack
  • Announcement.
  • Programming Assignment, due next week

3
C Templates
  • Parameterized data types
  • Type-safe macros
  • Come in two types
  • Class Templates
  • Function Templates

4
Class Templates
  • Can specialize class (see listing 1)
  • template ltclass Tgt class Stack
  • Stackltclass Messagegt
  • Or any other type
  • Stackltintgt

5
Class Template Member Functions
  • Method body must be in .h file

6
Using a Class Template
  • See listing 2
  • What is the output?

7
Function Templates
  • Specialize a function to take polymorphic
    arguments
  • Template ltclass Tgt T max(T a, T b)
  • But be careful with pointer types
  • See listing 3
  • What does it print?

8
Template Function Specialization
  • To fix problem, use type-specific version
  • templateltgt char max(char a, char b) return
    strcmp(a, b) gt 0 ? a b

9
Standard Template Library (STL)
  • Generic Programming
  • specify algorithms and data structures that work
    with any data type
  • Core Components
  • Containers
  • Algorithms
  • Iterators

10
STL Containers
  • Data structures that manage a set of memory
    locations
  • Doesnt contain many member functions
  • Creating, copying, destroying, adding, removing
  • No pointers to elements
  • Algorithms do the day-to-day stuff

11
STL Iterators
  • Used to traverse elements of containers
  • Uniform set/naming across containers
  • Algorithms designed to work with a particular
    iterator category

Random Access
Bi-Directional
Forward
Output
Input
12
STL Algorithms
  • Decoupled from containers
  • Parameterized by iterator types
  • Algorithm categories
  • Non-mutating sequence operations
  • Mutating sequence operations
  • Searching and Sorting
  • Set Operations
  • Heap operations
  • Numeric operations
  • Miscellaneous

13
Orthogonal Component Structure
  • So how does this all work together?
  • vectorltintgt v(3)v0 7v1 v0 3v2
    v0 v1reverse(v.begin(), v.end())
  • So what kind of components are v, v.begin(), and
    reverse?

Algorithm
Iterator
Container
14
Example STL deque
  • Double-Ended QUEue
  • Supports
  • Random access to elements
  • Constant time insertion removal of elements _at_
    end
  • Linear time insertion and removal of elements in
    the middle
  • Constant time insertion removal of elements _at_
    beginning
  • What role would the template parameter to deque
    fulfill?

15
Example STL deque
  • Use
  • dequeltintgt QQ.push_back(3)Q.push_front(1)Q.i
    nsert(Q.begin() 1, 2)Q2
    0copy(Q.begin(), Q.end(), ostream
    iteratorltintgt(cout, ))
  • What does this do?

16
Example STL stackltT, Sequencegt
  • Adaptor that supports restricted subset of
    Container functionality
  • Insertion, removal, and inspection of element at
    the top of the stack
  • Does not allow iteration through its elements

17
Example STLstackltT, Sequencegt
  • Example use
  • int main() stackltintgt S S.push(8)
    S.push(7) S.push(4) assert(S.size() 3)
    assert(S.top() 4) S.pop()
    assert(S.top() 7) S.pop() assert(S.top()
    8) S.pop() assert(S.empty())

18
Containers
  • Sequences
  • vector, deque list, slist, bit_vector
  • Associative Containers
  • set, map, multiset, multimap, hash_set, hash_map,
    hash_multiset, hash_multimap, hash
  • String package
  • char_traits, basic_string,
  • rope
  • Container adaptors
  • stack, queue, priority_queue, bitset

19
Algorithms
  • Non-mutating algorithms
  • for_each, find,, count, mismatch, equal, search
  • Mutating algorithms
  • copy, swap, transform, replace, fill, generate,
    remove, unique, reverse, rotate, random_shuffle,
    random_sample, partition, stable_partition,
    sorting, nth_element, binary search, merge, set
    operations heap operations, min and max,
    lexicographical_compare, permutations
  • Generalized numeric algorithms
  • iota, accumulate, inner_product, partial_sum,
    adjacent_difference, power

20
Iterators
  • Iterator classes
  • istream_iterator
  • ostream_iterator
  • front_insert_iterator
  • back_insert_iterator
  • insert_iterator
  • reverse_iterator
  • reverse_bidirectional_iterator
  • raw_storage_iterator
  • sequence_buffer
Write a Comment
User Comments (0)
About PowerShow.com