California State University Fresno Introduction to Data Structure

  • Slides: 15
Download presentation
California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of

California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer Science California State University, Fresno Fall 2006 Introduction to Data Structure, Fall 2006 Slide- 1

California State University, Fresno Vector STL - Constructors vector(); Create an empty vector. This

California State University, Fresno Vector STL - Constructors vector(); Create an empty vector. This is the default constructor. vector(int n, const T& value = T()); Create a vector with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). vector(T *first, T *last); Initialize the vector using the address range [first, last). Introduction to Data Structure, Fall 2006 Slide- 2

California State University, Fresno Vector STL - Access T& back(); Return the value of

California State University, Fresno Vector STL - Access T& back(); Return the value of the item at the rear of the vector. Precondition: The vector must contain at least one element. const T& back() const; Constant version of back(). bool empty() const; Return true if the vector is empty and false otherwise. Introduction to Data Structure, Fall 2006 Slide- 3

California State University, Fresno Vector STL - Access T& operator[] (int i); Allow the

California State University, Fresno Vector STL - Access T& operator[] (int i); Allow the vector element at index i to be retrieved or modified. Precondition: The index, i, must be in the range 0 i < n, where n is the number of elements in the vector. Postcondition: If the operator appears on the left side of an assignment statement, the expression on the right side modifies the element referenced by the index. const T& operator[] (int i) const; Constant version of the index operator. Introduction to Data Structure, Fall 2006 Slide- 4

California State University, Fresno Vector STL – Insertion/Removal void push_back(const T& value); Add a

California State University, Fresno Vector STL – Insertion/Removal void push_back(const T& value); Add a value at the rear of the vector. Postcondition: The vector has a new element at the rear and its size increases by 1. void pop_back(); Remove the item at the rear of the vector. Precondition: The vector is not empty. Postcondition: The vector has a new element at the rear or is empty. How to insert/remove at the middle? Introduction to Data Structure, Fall 2006 Slide- 5

California State University, Fresno Vector STL – Growing/Shrinking void resize((int n, const T& fill

California State University, Fresno Vector STL – Growing/Shrinking void resize((int n, const T& fill = T()); Modify the size of the vector. If the size is increased, the value fill is added to the elements on the tail of the vector. If the size is decreased, the original values at the front are retained. Postcondition: The vector has size n. int size() const; Return the number of elements in the vector Introduction to Data Structure, Fall 2006 Slide- 6

California State University, Fresno Example – Printing a vector template <typename T> void write.

California State University, Fresno Example – Printing a vector template <typename T> void write. Vector(const vector<T>& v) { // capture the size of the vector in n int i, n = v. size(); for(i = 0; i < n; i++) cout << v[i] << " "; cout << endl; } Introduction to Data Structure, Fall 2006 Slide- 7

California State University, Fresno Using Vector - Declaration #include <vector> using namespace std; //

California State University, Fresno Using Vector - Declaration #include <vector> using namespace std; // vector of size 5 containing the integer value 0 vector<int> int. Vector(5); 0 0 0 1 0 2 0 3 0 4 // vector of size 10; each element contains the empty string vector<string> str. Vector(10); Introduction to Data Structure, Fall 2006 Slide- 8

California State University, Fresno Using Vector – Insertion/Removal Write code to remove {8} from

California State University, Fresno Using Vector – Insertion/Removal Write code to remove {8} from a vector {12, -5, 8, 14, 10} Introduction to Data Structure, Fall 2006 Slide- 9

California State University, Fresno Using Vector – Resizing vector<int> v(5); 7 4 9 3

California State University, Fresno Using Vector – Resizing vector<int> v(5); 7 4 9 3 1 9 3 v. resize(10); 7 4 0 0 0 v. resize(4); 7 4 Introduction to Data Structure, Fall 2006 Slide- 10

California State University, Fresno Application – Insertion Sort Monroe Start with Monroe Insert Chin

California State University, Fresno Application – Insertion Sort Monroe Start with Monroe Insert Chin in location 0; Monroe moves to location 1 Chin Monroe Processing Flores Chin Flores Monroe Insert Flores in location 1; Monroe moves to location 2 Processing Stein Chin Flores Monroe Stein Processing Chin Processing Dare Chin Dare Flores Monroe Element Stein is OK Stein Insert Dare at location 1; the tail of the list shifts to the right Introduction to Data Structure, Fall 2006 Slide- 11

California State University, Fresno Insertion Sort - Algorithm for each element i in vector

California State University, Fresno Insertion Sort - Algorithm for each element i in vector v shifting all larger elements before i one position right; inserting element i to the right position; Introduction to Data Structure, Fall 2006 Slide- 12

California State University, Fresno Insertion Sort - Algorithm for each element i in vector

California State University, Fresno Insertion Sort - Algorithm for each element i in vector v store v[i] to temp; for all previous element j from 1 to i if v[j] > temp shift element j to element j+1 record the last shifting element to k inserting element i at position k; Introduction to Data Structure, Fall 2006 Slide- 13

California State University, Fresno Insertion Sort - Code insertion. Sort(): template <typename T> void

California State University, Fresno Insertion Sort - Code insertion. Sort(): template <typename T> void insertion. Sort(vector<T>& v) { int i, j, n = v. size(); T temp; for (i = 1; i < n; i++) { j = i; temp = v[i]; while (j > 0 && temp < v[j-1]) { v[j] = v[j-1]; j--; } v[j] = temp; } } Introduction to Data Structure, Fall 2006 Slide- 14

California State University, Fresno Example – Tracing the program aedcb j=1, temp=e skip j=2,

California State University, Fresno Example – Tracing the program aedcb j=1, temp=e skip j=2, temp=d since d < v[1], v[1] = d , j=1 (a, e, e, c, b) j=1, temp=d skip (a, d, e, c, b) j=3, temp=c since c<v[2], v[2]=c, j=2 (a, d, e, e, b) j=2, temp=c since c<v[1], v[1]=c, j=1 (a, d, d, e, b) j=1, temp=c skip (a, c, d, e, b) Next? Introduction to Data Structure, Fall 2006 Slide- 15