Object Oriented Programming Using C CLASS 8 Honors 1 Objectives Become familiar with the STL functionalities To understand how algorithms use iterators to access the elements of STL containers Use a templatized STL container 2 16 5 Introduction to the Standard Template Library STL The Standard Template Library contains many templates for useful algorithms and data structures STL contains numerous generic templates for implementing abstract data types and algorithms 3 Abstract Data Types The most important data structures in the STL are containers and iterators A container is a class that stores data and organizes it in some fashion An iterator is like a pointer It is used to access the individual data elements in a container 4 Standard Template Library STL Three basic components containers a collection of objects algorithms a function for processing a container such as sort search iterators mechanism for accessing the objects in a container 5 Advantages STL containers grow and shrink in size automatically contribute to programming ease use storage more efficiently can be subclassed to provide additional functionality allow the programmer to avoid the use of pointer new delete promote reusability and extensibility Can contain built in or user defined data types 6 Disadvantages STL Learning what is available and how to use it Implemented differently on different machines The syntax can be messy and ambiguous Care needs to be taken so that the container classes work on arbitrary subclasses 7 Container a data structure seven basic ones divided into two groups sequential containers elements can be access by position list vector deque 8 Container a data structure associative containers elements can be access by key map multimap set multiset 9 Algorithms on containers sort search replace reverse copy swap access an element insert an element erase an element 10 Vectors Like an array but grows and shrinks atuomatically to meet its own storage needs Assumes that the parametized type T has default and copy constructors as well as equality operator and a less than operator overloaded include vector 11 Container Classes The two types of container classes in the STL are sequence and associative A sequence container organizes data in a sequential fashion similar to an array An associative container uses keys to rapidly access elements 12 This program provides a simple demonstration of the vector STL template include iostream include vector Include the vector header using namespace std int main void int x vector int Vect Declare a vector object cout Vect starts with Vect size elements n 13 Use push back to push values into the vector for x 0 x 10 x Vect push back x cout Now Vect has Vect size elements Here they are n Use the operator for x 0 x Vect size x cout Vect x cout endl 14 Use the pop back member function cout Popping the values out of Vect n for x 0 x 10 x Vect pop back cout Now Vect has Vect size elements n 15 This program provides a simple demonstration of an iterator include iostream include vector Include the vector header using namespace std int main void int x vector int Vect Declare a vector object vector int iterator Iter Declare an iterator 16 Use push back to push values into the vector for x 0 x 10 x Vect push back x and use it to display the vector s contents cout Here are the values in Vect for Iter Vect begin Iter Vect end Iter cout Iter 17 cout nand here they are backwards for Iter Vect end 1 Iter Vect begin Iter cout Iter 18 This program provides a simple demonstration of the STL algorithms include iostream include vector include algorithm Required for STL algorithms using namespace std int main void int x vector int Vect for x 0 x 10 x Vect push back x 19 cout Vect has Vect size elements Here they are n for x 0 x Vect size x cout Vect x cout endl Randomly shuffle the vector s contents random shuffle Vect begin Vect end cout The elements have been shuffled n for x 0 x Vect size x cout Vect x cout endl Now sort them sort Vect begin Vect end 20 cout The elements have been sorted n for x 0 x Vect size x cout Vect x cout endl Now search for an element if binary search Vect begin Vect end 7 cout The value 7 was found in the vector n else cout The value 7 was not found in the vector n 21
View Full Document
Unlocking...