Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.

Similar presentations


Presentation on theme: "Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic."— Presentation transcript:

1

2 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic components  Common functions of containers  Iterators and stream iterators  More on &  Simple and popular algorithms -- By Rossella Lau

3 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Introduction to the STL  The STL can be thought of as the extension of C++  SL is carried from the traditional C library  STL comes with the C++  There are three basic components: containers, algorithms, and iterators

4 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Components of the STL  The Standard Template Library (STL) includes  Containers: e.g., arrays, vector<>, pair<> store multiple occurrences of data in a particular data structure provide methods to process on its particular structure  Algorithms: e.g., find(), sort(), copy(), remove() provide non-member methods for generic structures use with iterators on containers  Iterators: e.g., i:0..n-1 as the subscript of an array

5 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 General of containers  They are independent C++ template classes  Most are templates, only a few are inheritance, and no polymorphism  Consistent user-interface -- prototypes of member functions in different classes are as much the same as possible  All of the STL containers have the features of “true copy” or “deep copy”

6 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Types of Containers  Sequence Containers (sequential or fundamental containers); e.g. vector and list  Container Adapters; e.g., stack and queue;  Associated containers: e.g., set and map; they have special data structures  Others: e.g., array, string, pair<>

7 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 The useful pair<>  pair is a template class in the STL. It allows easy definition of some objects which have two members. E.g., for getProduct() in Catalog.h : pair getProduct(string code) const; To locate the members, e.g for displayProductInfo() in gourmetCoffee.cpp : pair result = readProduct(); if ( result.first ) {… result.second …}

8 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Typical operations of containers (I)  Adding/inserting elements  push_back(), insert()  for list<>: push_front(); which is not supported by vector<>  Accessing / Mutating  front(), back()  for : [index], at(index)  Sizing  size(), empty()  for : capacity(), resize()  Removal  pop_back(), pop_front(), erase(), clear()  for : remove(), remove_if()

9 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Return as a reference  For mutators, the return type usually is a reference  The reference is an additional (another) name of the variable returned so that it can be modified  Without reference, only the value of the variable is returned

10 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Rename with &  Sometimes, some variables are too long to identify, e.g., Person person = persons[indexPerson] ; person.remove();  The codes above cannot properly remove the item in person  It CAN if the first line becomes: Person & person = persons[indexPerson] ;

11 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Typical operations of containers (II)  Construction  Comparison  Miscellaneous  E.g., sort()  For iteration (return type is an iterator)  begin(), end(), rbegin(), rend() Online reference: http://www.sgi.com/tech/stl/ http://www.sgi.com/tech/stl/

12 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 A note on compilation  The compiler does not incorporate particular error messages but usually lists what types (classes) of a template are supported and makes the error message terribly long; as a beginner coding with the STL, be patient and persistent, you will get through it!

13 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5  An iterator is similar to the index for an array that leads a process to traverse from a position to another position of the container one by one  Each container provides its own iterators to allow generic algorithms to work on its elements; e.g., vector ::iterator  Typical iterators provided from each container:  begin().. end()  rbegin().. rend() // cannot be end().. begin(); While begin() refers to the first element of a container, end() refers to the position just after the last element -- this is also called a past-the-end iterator Iterator … begin() end()

14 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Iterator operations in C++  An iterator acts quite similar to a pointer in C++, it refers to a position of a container  Typical C++ iterator operations:  * de-reference the iterator as its pointing object   identify a member of an object pointed to by the iterator  ++ position the iterator to the next/previous element  == and != determine if two iterators are at the same position  = assign the position of an iterator to another iterator  E.g., in getProduct() of Catalog.h, for ( i = products.begin(); i != products.end() && (*i)->getCode() != code; i++);

15 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Typical loop with iterators  E.g., print the contents of a vector from the beginning:  E.g., print the contents of a vector from the end: vector studentID; vector ::iterator it = studentID.begin(); while ( it != studentID.end() ) { cout << *it++ << endl; vector studentID; vector ::iterator it = studentID.rbegin(); while ( it != studentID.rend() ) { cout << *it++ << endl;

16 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Some notes about Iterators  While begin() refers to the first element of a container, end() refers to the position just after the last element -- this is also called a past-the-end iterator  Not all containers allow the decrement operation "--" for iteration, to traverse a container in reverse order, the rbegin() and rend() should be used  Note that not all containers have reverse iterators  Since elements may not be positioned in the memory in a particular order, one should not determine if a container is not at the end by the checking of ( it < v.end() ) but (it ! = v.end())

17 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 STL algorithms  General accessing of algorithms, such as searching and sorting, are abstracted as a single class to work on different containers and usually work with iterators  Other classes of algorithms are for special purposes; e.g., numerical algorithms  Example usages:  find() and accumulate() in Order.h in the application gourmetCoffee  However, operator==() and operator+=(double, OrderItem const &) must be defined in OrderItem.h

18 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Typical algorithms E.g., (Assume v is a vector or an array, it is an iterator)  it = find(v.begin(), v.end(), x); // returns an iterator if ( it != v.end() ) // found  sort(v.begin(), v.end()); //if operator<() is/can be defined sort(v.begin(), v.end(), lessThanFunctionID * );  When < cannot be overloaded for the class as a member function nor a non member function (since there should be at least one operand in the type of the class), e.g., elements in a container are pointers – Catalog.h

19 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Typical Algorithms with stream iterators  Stream iterators istream_iterator (anIstream) – beginning of an input istream_iterator () – end of the input ostream_iterator (anOstream,delimiter) E.g.; In sample programs tryCopy1.cpp and tryCopy2.cpp istream_iterator inFileIt(inFile); istream_iterator inFileEof; ostream_iterator outFileIt(cout, "\n");  Using copy() to output (without a loop!) copy (v.begin(), v.end(), outFileIt);  Using copy() to display an input file to the screen copy(inFileIt, inFileEof, outFileIt);

20 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Summary  The basic components of the STL are containers, iterators, and algorithms  Operator overload and Iterators link the containers and algorithms together  The ability for an algorithm to process on a container depends on what kinds of iterators it provides  Coding using the STL is similar to writing a unique language -- it tries to eliminate explicit loop as much as possible

21 Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 Reference  Malik: Appendix H  Some On line reference sites: http://www.sgi.com/tech/stl/, http://www.cppreference.com/ http://www.sgi.com/tech/stl/ -- END --


Download ppt "Rossella Lau Lecture 12, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic."

Similar presentations


Ads by Google