STL - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

STL

Description:

Last week of class = project demos. 5-10 min. Show me what your project does ... virtual void baz() {this - foo();} class C: public A, public B { virtual void bar ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 22
Provided by: andrew45
Category:
Tags: stl

less

Transcript and Presenter's Notes

Title: STL


1
STL
  • CSE399
  • March 19, 2007

2
Announcements
  • Homework 5- Due Today
  • No homework this week
  • Work on your projects
  • Speaking of projects
  • Last week of class project demos
  • 5-10 min
  • Show me what your project does
  • Answer some questions (What C features
    helped? etc)

3
MI Clarification
  • class A .
  • class B
  • virtual void foo() .
  • virtual void baz() .
  • class C public A, public B
  • virtual void bar() .
  • virtual void foo() this-gtbar()

4
Calling b-gtfoo()
  • C c new C()
  • / c 100 /
  • B b c
  • / b 120 /
  • b-gtfoo()
  • / this 120
  • get foo from vtbl
  • and call it /

5
Calling c-gtbaz()
  • C c new C()
  • / c 100 /
  • c-gtbaz()
  • / C does not override baz(), baz() expects
    this to be a B. I.e.
  • B temp c
  • temp-gtbaz()
  • /

6
Suppose baz() calls foo()
  • class A .
  • class B
  • virtual void foo() .
  • virtual void baz() this -gtfoo()
  • class C public A, public B
  • virtual void bar() .
  • virtual void foo() this-gtbar()
  • ..
  • C c new C() / c 100 /
  • c-gtbaz()

7
Standard Template Library
  • Library of many useful containers / algorithms
  • Templated- can handle many types
  • In namespace std
  • Google for C STL for documentation

8
String class
  • Unlike C, C has string type
  • include ltstringgt
  • string s hello / uses string(const char )
    /
  • s
  • string s2 wor
  • s s2
  • s ld
  • cout ltlt s ltltendl
  • const char c s.c_str() / get C string /

9
Vectors
  • C has vectorltTgt
  • Similar in behavior (re-sizeable array) to Javas
  • Overloaded operators gt syntax like array
  • include ltvectorgt
  • vectorltintgt v
  • v.push_back(37) / Add to end /
  • v.push_back(12)
  • for (int i 0 i lt v.size() i)
  • cout ltlt v ltlt i ltlt ltlt vi ltltendl

10
Iterators
  • Position in container
  • vectorltintgtiterator it
  • for ( it v.begin() it ! v.end() it )
  • cout ltlt it ltlt endl
  • Type inside container (vectorltintgtiterator)
  • Many overloaded operators
  • go to next element
  • , ! compare positions
  • reference to element in container at given
    position

11
Algorithms
  • STL also has some common algorithms
  • include ltalgorithmgt
  • sort(v.begin(), v.end()) / sort all of v /
  • vectorltintgtiterator it
  • it find(v.begin(), v.end(), 14)
  • / it is an iterator with elements which 14 in
    v/

12
Iterators Algorithms
  • Notice sort find take iterators Why?
  • Iterator (Container position)
  • exactly the info sort/find need
  • Iterators provide a very generic interface
  • Avoids classes all inheriting from a superclass
  • Avoiding multiple inheritance good

13
Associative maps
  • STL also has associative maps
  • include ltmapgt
  • mapltstring, intgt mp
  • mpJan 1
  • mpFeb 2
  • mpMar 3
  • .
  • cout ltlt Mar is month ltlt mpMar ltlt endl
  • Allows for maps from any (reasonable) type to any
    other type

14
STL containee restrictions
  • Types in STL containers must have an accessible
    (defaults OK where applicable)
  • default constructor
  • destructor
  • assignment operator
  • copy constructor
  • Some things require inequality/equality operators

15
C casts
  • C style casts
  • x (T) y
  • C style casts
  • 4 kinds of casts, each with different behavior

16
Static cast
  • x static_castltTgt(y)
  • Most like C style casts
  • double d 3.0
  • int x static_castltdoublegt(d)
  • Checks only static (compile time) info for safety
  • int p static_castltint gt(d)
  • invalid static_cast from type double to type
    int

17
Const cast
  • x const_castltTgt (y)
  • Can only change constness
  • const char y abc
  • char x const_castltchar gt(y)
  • Note does not actually make x mutable
  • (i.e. x q may still result in a
    segfault)
  • and volatility

18
Dynamic Cast
  • x dynamic_castltTgt(y)
  • Checks for appropriate run time type when
    downcasting (Java like)
  • Gives NULL (when casting pointers) or an
    exception (when casting references) if invalid
    cast
  • class A
  • class B public A
  • A a1 new B()
  • A a2 new A()
  • B b1 dynamic_castltBgt(a1) / OK/
  • B b2 dynamic_castltBgt(a2) / NULL /

19
A side note
  • class A
  • class B public A
  • A a1 new B()
  • B b1 dynamic_castltBgt(a1)
  • error canot dynamic_cast a1 (of type class
    A) to type class B (source type is not
    polymorphic
  • Q What? A No virtual methods no vtbl

20
Reinterpret Cast
  • x reinterpret_castltTgt(y)
  • Basically C style cast. Allows things like
  • int p reinterpret_castltint gt(42)
  • But screams Im not safe!

21
Questions?
  • Any questions?
  • Also, requests for topics?
  • 3 lectures remain
  • Have covered major topics in C
  • Want something re-covered? Something else
    discussed? E-mail me.
Write a Comment
User Comments (0)
About PowerShow.com