
Chapter 21 - Standard Template Library 1 (STL) Outline 21.1 Introduction to the Standard Template Library (STL) 21.1.1 Introduction to Containers 21.1.2 Introduction to Iterators 21.1.3 Introduction to Algorithms 21.2 Sequence Containers 21.2.1 vector Sequence Container 21.2.2 list Sequence Container 21.2.3 deque Sequence Container 21.3 Associative Containers 21.3.1 multiset Associative Container 21.3.2 set Associative Container 21.3.3 multimap Associative Container 21.3.4 map Associative Container 21.4 Container Adapters 21.4.1 stack Adapter 21.4.2 queue Adapter 21.4.3 priority_queue Adapter 2003 Prentice Hall, Inc. All rights reserved. Chapter 21 - Standard Template Library 2 (STL) 21.5 Algorithms 21.5.1 fill, fill_n, generate and generate_n 21.5.2 equal, mismatch and lexicographical_compare 21.5.3 remove, remove_if, remove_copy and remove_copy_if 21.5.4 replace, replace_if, replace_copy and replace_copy_if 21.5.5 Mathematical Algorithms 21.5.6 Basic Searching and Sorting Algorithms 21.5.7 swap, iter_swap and swap_ranges 21.5.8 copy_backward, merge, unique and reverse 21.5.9 inplace_merge, unique_copy and reverse_copy 21.5.10 Set Operations 21.5.11 lower_bound, upper_bound and equal_range 21.5.12 Heapsort 21.5.13 min and max 21.5.14 Algorithms Not Covered in This Chapter 21.6 Class bitset 21.7 Function Objects 2003 Prentice Hall, Inc. All rights reserved. 3 21.1 Introduction to the Standard Template Library (STL) • STL – Powerful, template-based components • Containers: template data structures • Iterators: like pointers, access elements of containers • Algorithms: data manipulation, searching, sorting, etc. – Object- oriented programming: reuse, reuse, reuse – Only an introduction to STL, a huge class library 2003 Prentice Hall, Inc. All rights reserved. 4 21.1.1 Introduction to Containers • Three types of containers – Sequence containers • Linear data structures (vectors, linked lists) • First-class container – Associative containers • Non-linear, can find elements quickly • Key/value pairs • First-class container – Container adapters • Near containers – Similar to containers, with reduced functionality • Containers have some common functions 2003 Prentice Hall, Inc. All rights reserved. 5 STL Container Classes (Fig. 21.1) • Sequence containers – vector – deque – list • Associative containers – set – multiset – map – multimap • Container adapters – stack – queue – priority_queue 2003 Prentice Hall, Inc. All rights reserved. 6 Common STL Member Functions (Fig. 21.2) • Member functions for all containers – Default constructor, copy constructor, destructor – empty – max_size, size – = < <= > >= == != – swap • Functions for first-class containers – begin, end – rbegin, rend – erase, clear 2003 Prentice Hall, Inc. All rights reserved. 7 Common STL typedefs (Fig. 21.4) • typedefs for first-class containers – value_type – reference – const_reference – pointer – iterator – const_iterator – reverse_iterator – const_reverse_iterator – difference_type – size_type 2003 Prentice Hall, Inc. All rights reserved. 8 21.1.2 Introduction to Iterators • Iterators similar to pointers – Point to first element in a container – Iterator operators same for all containers • * dereferences • ++ points to next element • begin() returns iterator to first element • end() returns iterator to last element – Use iterators with sequences (ranges) • Containers • Input sequences: istream_iterator • Output sequences: ostream_iterator 2003 Prentice Hall, Inc. All rights reserved. 9 21.1.2 Introduction to Iterators • Usage – std::istream_iterator< int > inputInt( cin ) • Can read input from cin • *inputInt – Dereference to read first int from cin • ++inputInt – Go to next int in stream – std::ostream_iterator< int > outputInt(cout) • Can output ints to cout • *outputInt = 7 – Outputs 7 to cout • ++outputInt – Advances iterator so we can output next int 2003 Prentice Hall, Inc. All rights reserved. 10 1 // Fig. 21.5: fig21_05.cpp Outline 2 // Demonstrating input and output with iterators. 3 #include <iostream> 4 fig21_05.cpp 5 using std::cout; (1 of 2) 6 using std::cin; 7 using std::endl; 8 Note creation of 9 #include <iterator> // ostream_iteratoristream_iterator and istream_iterator. For 10 compilation reasons, we use 11 int main() std:: rather than a using 12 { statement. 13 cout << "Enter two integers: "; Access and assign the iterator 14 like a pointer. 15 // create istream_iterator for reading int values from cin 16 std::istream_iterator< int > inputInt( cin ); 17 18 int number1 = *inputInt; // read int from standard input 19 ++inputInt; // move iterator to next input value 20 int number2 = *inputInt; // read int from standard input 21 2003 Prentice Hall, Inc. All rights reserved. 11 22 // create ostream_iterator for writing int values to cout Outline 23 std::ostream_iterator< int > outputInt( cout ); 24 25 cout << "The sum is: "; fig21_05.cpp 26 *outputInt = number1 + number2; // output result to cout (2 of 2) 27 cout << endl; 28 fig21_05.cpp 29 return 0; output (1 of 1) 30 31 } // end main Enter two integers: 12 25 The sum is: 37 Create an ostream_iterator is similar. Assigning to this iterator outputs to cout. 2003 Prentice Hall, Inc. All rights reserved. 12 Iterator Categories (Fig. 21.6) • Input – Read elements from container, can only move forward • Output – Write elements to container, only forward • Forward – Combines input and output, retains position – Multi-pass (can pass through sequence twice) • Bidirectional – Like forward, but can move backwards as well • Random access – Like bidirectional, but can also jump to any element 2003 Prentice Hall, Inc. All rights reserved. 13 Iterator Types Supported (Fig. 21.8) • Sequence containers – vector: random access – deque: random access – list: bidirectional • Associative containers (all bidirectional) – set – multiset – Map – multimap • Container adapters (no iterators supported) – stack – queue – priority_queue 2003 Prentice Hall, Inc. All rights reserved. 14 Iterator Operations (Fig. 21.10) • All – ++p, p++ • Input iterators – *p – p = p1 – p == p1, p != p1 • Output iterators – *p – p = p1 • Forward iterators – Have functionality of input and output iterators 2003 Prentice Hall, Inc. All rights reserved. 15 Iterator Operations (Fig. 21.10) • Bidirectional – --p, p-- • Random access – p + i, p += i – p - i, p -= i – p[i] – p < p1, p <= p1 – p > p1, p >= p1 2003 Prentice Hall, Inc. All rights reserved. 16 21.1.3 Introduction to Algorithms • STL has algorithms used generically across containers – Operate on elements indirectly via iterators – Often operate on sequences of elements • Defined by pairs of iterators • First and last element – Algorithms often return iterators • find() • Returns iterator to element, or end() if not found – Premade algorithms save programmers time and effort 2003 Prentice Hall, Inc. All rights reserved. 17 21.2 Sequence Containers • Three sequence containers – vector - based on arrays – deque - based on arrays – list - robust linked list 2003 Prentice Hall, Inc. All rights reserved. 18 21.2.1 vector Sequence Container • vector – <vector> – Data structure with contiguous memory locations • Access elements with [] – Use when data must be sorted and easily accessible • When memory exhausted – Allocates larger, contiguous area of memory – Copies itself there – Deallocates old memory • Has random access iterators 2003 Prentice Hall, Inc. All rights reserved. 19 21.2.1 vector Sequence Container • Declarations – std::vector <type> v; • type: int, float, etc. • Iterators – std::vector<type>::const_iterator iterVar; • const_iterator cannot modify elements – std::vector<type>::reverse_iterator iterVar; • Visits elements in reverse order (end to beginning) • Use rbegin to get starting point • Use rend to get ending point 2003 Prentice Hall, Inc. All rights reserved. 20 21.2.1 vector Sequence Container • vector functions – v.push_back(value) • Add element to end (found in all sequence containers). – v.size() • Current size of vector – v.capacity() • How much vector can hold before reallocating memory • Reallocation doubles size – vector<type> v(a, a + SIZE) • Creates vector v with elements from array a up to (not including) a + SIZE 2003 Prentice Hall, Inc. All rights reserved. 21 21.2.1 vector Sequence Container • vector functions – v.insert(iterator, value ) • Inserts value before location of iterator – v.insert(iterator, array , array + SIZE) • Inserts array elements (up to, but not including array + SIZE) into vector – v.erase( iterator ) • Remove element from container – v.erase( iter1, iter2 ) • Remove elements starting from iter1 and up to (not including) iter2 – v.clear() • Erases entire container 2003 Prentice Hall, Inc. All rights reserved. 22 21.2.1 vector Sequence Container • vector functions operations – v.front(), v.back() • Return first and last element – v.[elementNumber] = value; • Assign value to an element – v.at[elementNumber] = value; • As above, with range checking • out_of_bounds exception 2003 Prentice Hall, Inc. All rights reserved. 23 21.2.1 vector Sequence Container • ostream_iterator – std::ostream_iterator< type > Name ( outputStream, separator ); • type: outputs values of a certain type • outputStream: iterator output location • separator: character separating outputs • Example – std::ostream_iterator< int > output( cout, " " ); – std::copy( iterator1, iterator2, output ); • Copies elements from iterator1 up to (not including) iterator2 to output, an ostream_iterator 2003 Prentice Hall, Inc. All rights reserved. 24 1 // Fig.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages155 Page
-
File Size-