 2003 Prentice Hall, Inc. All rights reserved. 1 21.2.1 vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.

Slides:



Advertisements
Similar presentations
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Advertisements

Beginning C++ Through Game Programming, Second Edition
4.1Introduction Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based arrays (C-like) –Arrays.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to the Standard Template Library (STL) STL –Useful template-based components Containers:
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Templates Outlines 1. Introduction 2. Function Templates 3. Overloading Function Templates 4. Class Templates.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Dr. Yingwu Zhu STL Vector and Iterators. STL (Standard Template Library) 6:14:43 AM 2 A library of class and function templates Components: 1. Containers:
 2003 Prentice Hall, Inc. All rights reserved stack Adapter stack –Header –Insertions and deletions at one end –Last-in, first-out (LIFO) data.
Templates Zhen Jiang West Chester University
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Data Structures Using C++ 2E
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
 2003 Prentice Hall, Inc. All rights reserved Introduction to the Standard Template Library (STL) STL –Powerful, template-based components Containers:
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools File Processing, Standard Template Library Lecture 12 July.
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
A gentle introduction to the standard template library (STL) Some references:
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Standard Template Library Lecture 9 March 22, 2005.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Standard Template Library Lecture 12 April 6, 2004.
Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library (STL) Introduction to Containers Introduction.
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Vectors Updated 11/4/2003 by Kip Irvine. Copyright Kip Irvine Overview What is a vector? Declaring vector objects Inserting and removing items Using.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Vectors Holds a set of elements, like an array
CSCE 210 Data Structures and Algorithms
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Vectors.
Chapter 21 - Standard Template Library (STL)
4.9 Multiple-Subscripted Arrays
7. 11 Introduction to C++ Standard Library Class Template vector (Cont
Class string and String Stream Processing
Chapter 21 - Standard Template Library (STL)
Chapter 21 - Standard Template Library (STL)
Arrays Arrays A few types Structures of related data items
Chapter 21 - Standard Template Library (STL)
4.1 Introduction Arrays A few types Structures of related data items
A dictionary lookup mechanism
Chapter 21 - Standard Template Library (STL)
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector ::const_iterator iterVar; const_iterator cannot modify elements –std::vector ::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 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 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 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 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 vector Sequence Container ostream_iterator –std::ostream_iterator Name( outputStream, separator ); type : outputs values of a certain type outputStream : iterator output location separator : character separating outputs Example –std::ostream_iterator 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. Outline 6 fig21_14.cpp (1 of 3) 1 // Fig : fig21_14.cpp 2 // Demonstrating standard library vector class template. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include // vector class-template definition // prototype for function template printVector 12 template 13 void printVector( const std::vector &integers2 ); int main() 16 { 17 const int SIZE = 6; 18 int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; std::vector integers; cout << "The initial size of integers is: " 23 << integers.size() 24 << "\nThe initial capacity of integers is: " 25 << integers.capacity(); 26 Create a vector of int s. Call member functions.

 2003 Prentice Hall, Inc. All rights reserved. Outline 7 fig21_14.cpp (2 of 3) 27 // function push_back is in every sequence collection 28 integers.push_back( 2 ); 29 integers.push_back( 3 ); 30 integers.push_back( 4 ); cout << "\nThe size of integers is: " << integers.size() 33 << "\nThe capacity of integers is: " 34 << integers.capacity(); cout << "\n\nOutput array using pointer notation: "; for ( int *ptr = array; ptr != array + SIZE; ++ptr ) 39 cout << *ptr << ' '; cout << "\nOutput vector using iterator notation: "; 42 printVector( integers ); cout << "\nReversed contents of vector integers: "; 45 Add elements to end of vector using push_back.

 2003 Prentice Hall, Inc. All rights reserved. Outline 8 fig21_14.cpp (3 of 3) 46 std::vector ::reverse_iterator reverseIterator; for ( reverseIterator = integers.rbegin(); 49 reverseIterator!= integers.rend(); 50 ++reverseIterator ) 51 cout << *reverseIterator << ' '; cout << endl; return 0; } // end main // function template for outputting vector elements 60 template 61 void printVector( const std::vector &integers2 ) 62 { 63 std::vector ::const_iterator constIterator; for ( constIterator = integers2.begin(); 66 constIterator != integers2.end(); 67 constIterator++ ) 68 cout << *constIterator << ' '; } // end function printVector Walk through vector backwards using a reverse_iterator. Template function to walk through vector forwards.

 2003 Prentice Hall, Inc. All rights reserved. Outline 9 fig21_14.cpp output (1 of 1) The initial size of v is: 0 The initial capacity of v is: 0 The size of v is: 3 The capacity of v is: 4 Contents of array a using pointer notation: Contents of vector v using iterator notation: Reversed contents of vector v: 4 3 2

 2003 Prentice Hall, Inc. All rights reserved. Outline 10 fig21_15.cpp (1 of 3) 1 // Fig : fig21_15.cpp 2 // Testing Standard Library vector class template 3 // element-manipulation functions. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 #include // vector class-template definition 10 #include // copy algorithm int main() 13 { 14 const int SIZE = 6; 15 int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; std::vector integers( array, array + SIZE ); 18 std::ostream_iterator output( cout, " " ); cout << "Vector integers contains: "; 21 std::copy( integers.begin(), integers.end(), output ); cout << "\nFirst element of integers: " << integers.front() 24 << "\nLast element of integers: " << integers.back(); 25 Create vector (initialized using an array) and ostream_iterator. Copy range of iterators to output ( ostream_iterator ).

 2003 Prentice Hall, Inc. All rights reserved. Outline 11 fig21_15.cpp (2 of 3) 26 integers[ 0 ] = 7; // set first element to 7 27 integers.at( 2 ) = 10; // set element at position 2 to // insert 22 as 2nd element 30 integers.insert( integers.begin() + 1, 22 ); cout << "\n\nContents of vector integers after changes: "; 33 std::copy( integers.begin(), integers.end(), output ); // access out-of-range element 36 try { 37 integers.at( 100 ) = 777; } // end try // catch out_of_range exception 42 catch ( std::out_of_range outOfRange ) { 43 cout << "\n\nException: " << outOfRange.what(); } // end catch // erase first element 48 integers.erase( integers.begin() ); 49 cout << "\n\nVector integers after erasing first element: "; 50 std::copy( integers.begin(), integers.end(), output ); 51 More vector member functions. at has range checking, and can throw an exception.

 2003 Prentice Hall, Inc. All rights reserved. Outline 12 fig21_15.cpp (3 of 3) 52 // erase remaining elements 53 integers.erase( integers.begin(), integers.end() ); 54 cout << "\nAfter erasing all elements, vector integers " 55 << ( integers.empty() ? "is" : "is not" ) << " empty"; // insert elements from array 58 integers.insert( integers.begin(), array, array + SIZE ); 59 cout << "\n\nContents of vector integers before clear: "; 60 std::copy( integers.begin(), integers.end(), output ); // empty integers; clear calls erase to empty a collection 63 integers.clear(); 64 cout << "\nAfter clear, vector integers " 65 << ( integers.empty() ? "is" : "is not" ) << " empty"; cout << endl; return 0; } // end main

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig21_15.cpp output (1 of 1) Vector integers contains: First element of integers: 1 Last element of integers: 6 Contents of vector integers after changes: Exception: invalid vector subscript Vector integers after erasing first element: After erasing all elements, vector integers is empty Contents of vector integers before clear: After clear, vector integers is empty