Week 5 - Week 5 1 Chapter 10: The Standard Template Library...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Week 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 10: The Standard Template Library Outline: C++ Standard Library STL Iterators Containers Adapters: stacks, queues, priority queues. Sequence Containers: Vector, List Associative Containers: map, multimap, set, multiset. Algorithms 2
Background image of page 2
C++ Standard Library The C++ standard library provides many useful routines for mathematics, I/O, etc. To use the routines in the standard library the relevant header files must be included in the user source files before compilation. Some routines in C++ standard library correspond to C standard library. The C++ header file is < c XYZ> if the corresponding C header file is <XYZ .h >. A significant part of the standard library uses templates and is called the C++ S tandard T emplate L ibrary ( STL ). 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Standard Library Categories 4 http://msdn.microsoft.com/en-us/library/a7tkse1h(vs.80).aspx
Background image of page 4
Prototypes of Standard Library Functions The system header file iostream already contains the prototypes of many standard library functions. Thus it may not be necessary to include the specific header file for a particular library function’s prototype. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Standard Library Functions Example (1) The simple random number generator rand () generates a sequence of non-negative random numbers (< RAND_MAX). When the same seed is used in calling srand (seed), rand () will produce the same sequence of random numbers. 6
Background image of page 6
Standard Library Functions Example (2) $ cat rand.cpp #include <iostream> using namespace std; int main(int ac, char *av[]) { int seed = 0; if(ac>1) seed = atoi(av[1]); srand(seed) ; int i, c[10], N=1 << 20; for(i=0; i<10; i++) c[i] = 0 ; for(i=0; i<N; i++) c[ rand () % 10]++; for(i=0; i<10; i++) cout << c[i]/ float (N) << ’\n’; } 7 $ g++ rand.cpp $ a.out 0.0999527 0.100307 0.0999861 0.0997295 0.0998964 0.0998697 0.100133 0.100414 0.100032 0.099679
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Standard Library Functions Example (3) $ cat ctype.cpp #include <iostream> using namespace std; int main(int ac, char *av[]) { int c; if(ac<2) return -1; else c = atoi(av[1]); if( isprint (c)) cout << char(c) << ": "; else cout << "code " << c << " nonprintable"; 8
Background image of page 8
Standard Library Functions Example (5) if( isalnum (c)) cout << " isalnum"; if( isalpha (c)) cout << " isalpha"; if( iscntrl (c)) cout << " iscntrl"; if( isdigit (c)) cout << " isdigit"; if( isgraph (c)) cout << " isgraph"; // isprint() exclude blank if( islower (c)) cout << " islower"; if( isprint (c)) cout << " isprint"; if( ispunct (c)) cout << " ispunct"; if( isspace (c)) cout << " isspace"; if( isupper (c)) cout << " isupper"; if( isxdigit (c)) cout << " isxdigit"; cout << endl; } 9
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example (6) $ for c in 33 44 55 66 77 88 99 > do > a.out $c > done !: isgraph isprint ispunct ,: isgraph isprint ispunct 7: isalnum isdigit isgraph isprint isxdigit B: isalnum isalpha isgraph isprint isupper isxdigit M: isalnum isalpha isgraph isprint isupper X: isalnum isalpha isgraph isprint isupper c: isalnum isalpha isgraph islower isprint isxdigit 10
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/02/2011 for the course CS 1280 taught by Professor Forgotten during the Spring '09 term at National University of Singapore.

Page1 / 66

Week 5 - Week 5 1 Chapter 10: The Standard Template Library...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online