Generic Programming Techniques in C - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

Generic Programming Techniques in C

Description:

Generic programming allows diverse types to be used together Without requiring inheritance relations among them ... Generic Programming Techniques in C++ – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 6
Provided by: wus92
Category:

less

Transcript and Presenter's Notes

Title: Generic Programming Techniques in C


1
Generic Programming Techniques in C
  • Generic programming allows diverse types to be
    used together
  • Without requiring inheritance relations among
    them
  • Lets native, library, and user-defined types work
    with each other well
  • Generic abstractions rely on interface
    polymorphism
  • Based on C templates, typedefs, and compilers
    type inference rules
  • Allow many combinations to be composed
    successfully
  • Impose specific rules on types interfaces
  • Today well look at some examples for why this is
    useful
  • To motivate investment of effort towards a deeper
    understanding
  • To show some early techniques that you can apply
    right away
  • Subsequent lectures and studios will dig into
    this in more detail
  • Well look first at key generic abstractions the
    STL provides
  • Containers that can hold diverse types
  • Iterators that define ranges, act like built-in
    pointer types
  • Algorithms that use iterators to manipulate data
    in containers
  • Function objects that are used to extend
    containers and algorithms

2
STL Containers Can Hold Different Types
  • include ltvectorgt
  • include ltiostreamgt
  • using namespace std
  • int main (int, char )
  • vectorltchar gt cstrings
  • vectorltintgt numbers
  • cstrings.push_back(one)
  • cstrings.push_back(two)
  • numbers.push_back(1)
  • numbers.push_back(2)
  • cout ltlt cstrings0 ltlt endl ltlt cstrings1 ltlt
    endl
  • ltlt numbers0 ltlt endl ltlt numbers1 ltlt
    endl
  • return 0 // what output is produced, and why?

3
STL Iterators Can Act Like Pointers (Somewhat)
  • include ltvectorgt
  • include ltiostreamgt
  • using namespace std
  • int main (int, char )
  • vectorltchar gt cstrings
  • vectorltintgt numbers
  • cstrings.push_back(one) cstrings.push_back(t
    wo)
  • numbers.push_back(1) numbers.push_back(2)
  • vectorltchar gtiterator cstrings_iter

  • cstrings.begin()

  • vectorltintgtiterator numbers_iter
    numbers.begin()
  • cout ltlt cstrings_iter ltlt endl

  • cstrings_iter

  • cout ltlt cstrings_iter ltlt endl

  • cout ltlt numbers_iter ltlt endl

  • numbers_iter

  • cout ltlt numbers_iter ltlt endl
  • return 0 // what output is produced, and why?

4
STL Algorithms Use Iterators to Manipulate Data
  • include ltvectorgt
  • include ltiostreamgt
  • include ltalgorithmgt
  • include ltiteratorgt
  • using namespace std
  • int main (int, char )
  • vectorltchar gt cstrings
  • vectorltintgt numbers
  • cstrings.push_back(one)
  • cstrings.push_back(two)
  • numbers.push_back(1)
  • numbers.push_back(2)
  • copy (cstrings.begin(), cstrings.end(),
  • ostream_iteratorltchar gt(cout, \n))
  • copy (numbers.begin(), numbers.end(),
  • ostream_iteratorltintgt(cout, \n))
  • return 0 // what output is produced, and why?

5
STL Functors Extend Algorithms ( Containers)
  • include ltvectorgt
  • include ltiostreamgt
  • include ltalgorithmgt
  • include ltiteratorgt
  • using namespace std
  • int main (int, char )
  • vectorltchar gt cstrings
  • cstrings.push_back(one)
  • cstrings.push_back(two)
  • sort (cstrings.begin(), cstrings.end(),
  • greaterltchar gt())
  • copy (cstrings.begin(), cstrings.end(),
  • ostream_iteratorltchar gt(cout, \n))
  • return 0 // what output is produced, and why?
Write a Comment
User Comments (0)
About PowerShow.com