Standard Template Library - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Standard Template Library

Description:

Standard Template Library. STL. Classroom Project: Using example on page 587 build ... Concatenate: string str1('Now is the time for all good men ' ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 18
Provided by: lyon
Category:

less

Transcript and Presenter's Notes

Title: Standard Template Library


1
Standard Template Library
  • STL

2
Classroom Project
  • Using example on page 587 build a list of ten
    integers, 1-10.

3
Integer List
  • include ltiostreamgt
  • include ltlistgt
  • using namespace std
  • void main()
  • list ltintgt lst
  • int i
  • for (i0 ilt10 i) lst.push_back(i)
  • cout ltlt "Size " ltlt lst.size() ltlt endl
  • cout ltlt "Contents "
  • listltintgtiterator p lst.begin()
  • while(p ! lst.end())
  • cout ltlt p
  • p
  • cout ltlt endl

4
Algorithm Example - Sort
  • void main()
  • list ltintgt lst
  • int i
  • for (i0 ilt10 i) lst.push_back(rand()40)
  • cout ltlt "Original Contents "
  • listltintgtiterator p lst.begin()
  • while(p ! lst.end())
  • cout ltlt p ltlt endl
  • p
  • lst.sort()
  • cout ltlt "Sorted Contents "
  • p lst.begin()
  • while(p ! lst.end())
  • cout ltlt p ltlt endl
  • p
  • cout ltlt endl

5
Transform
  • int xform (int i) return i i
  • main()
  • . . .
  • p transform(lst.begin(), lst.end(),
    lst.begin(), xform)
  • . . .
  • //At this point each element of the list has been
    replaced
  • //by the square of its original value

6
Classroom Project
  • Generate a series of ten random numbers, print
    them out, then print out double their value using
    transform

7
Transform - Example
  • list ltintgt lst
  • int i
  • for (i0 ilt10 i) lst.push_back(rand()
    40)
  • cout ltlt "Original Contents " ltlt endl
  • listltintgtiterator p lst.begin()
  • while(p ! lst.end())
  • cout ltlt p ltlt endl
  • p
  • lst.sort()
  • cout ltlt "Sorted Contents " ltlt endl
  • p lst.begin()
  • while(p ! lst.end())
  • cout ltlt p ltlt endl
  • p
  • cout ltlt endl
  • p transform(lst.begin(), lst.end(),
    lst.begin(), xform)

8
Stack Implementation
  • 1. Write a program to put integer values into a
    stack using the STL class stack
  • 2. In a loop, put integers 53-63 in the stack
    using
  • a.push(i)
  • 3. Remove all elements from the stack using a
    combination of
  • top()
  • which returns the element at the top of the
    stack without removing it, and
  • pop()
  • which removes the element at the top of the
    stack, but returns nothing.

9
String Class
  • // Assume you are using three character strings
    of
  • // equal length
  • char s180, s280, s380
  • // Can you do this?
  • s1 one
  • s2 two
  • s3 s1 s2
  • //NO!! - not allowed
  • strcpy (s1, one)
  • strcpy (s2, two)
  • strcat (s3, s1)

10
String Class
  • See Page 611-613

11
String Class
  • Concatenate
  • string str1(Now is the time for all good men )
  • string str2(to come to the aid of their
    country.)
  • string str3
  • str3 str1 str2
  • Other string functions
  • insert()
  • erase()
  • replace()

12
String Class Exercise
  • Using combinations of strings str1 and str2
  • string str1(Now is the time for all good men )
  • string str2(to come to the aid of their
    country.)
  • As well as string 3
  • string str3( Miller )
  • And the functions below
  • insert()
  • erase()
  • replace()
  • Using the example on p. 617, print out this
    string
  • Now is Miller time for all good men

13
Project 12
  • Create member function to add elements to the
    list
  • add (T x) // adds element x to next
    //position in the list
  • del(T y) //delete element y from the list
  • printlist() // prints all elements of list
  • main()
  • listltintgt A
  • A.add(5)
  • A.add(7)
  • A.add(13)
  • A.del(7)
  • A.printlist()

14
List
  • template ltclass Tgt
  • class list
  • private
  • T values100
  • int number
  • public
  • list() number 0
  • list(int n, T val) number n
  • for (int i 1 i lt n i)
  • valuesi val
  • int size() return number
  • list()

15
A Better Class Definition
  • template ltclass Tgt
  • class list
  • private
  • T values100
  • int number
  • public
  • list() number 0
  • list(int n, T val) number n
  • for (int i 0 i lt n i)
  • valuesi val
  • void add(T x) valuesnumber x
  • number
  • void del(T x)
  • void printlist()
  • int size() return number
  • list()

16
Dont Forget...
  • template ltclass Tgt
  • void listltTgtdel(T x)
  • // Function code goes here
  • Dont use the word delete
  • Dont forget the template.

17
main()
  • void main()
  • listltfloatgt B
  • cout ltlt B.size()ltlt endl
  • listltintgt D(10, 100)
  • cout ltlt D.size() ltlt endl
  • B.add(3)
  • B.add(4)
  • D.add(1)
  • B.printlist()
  • D.printlist()
Write a Comment
User Comments (0)
About PowerShow.com