1 / 15

Introduction to Data Structure

Introduction to Data Structure. Chapter 4 Ming Li Department of Computer Science California State University, Fresno Fall 2006. Vector STL - Constructors. vector (); Create an empty vector. This is the default constructor. vector (int n, const T& value = T());

bbroyles
Download Presentation

Introduction to Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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).

  3. 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.

  4. 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.

  5. 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?

  6. 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

  7. Example – Printing a vector template <typename T> void writeVector(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; }

  8. 0 0 0 0 0 1 2 3 4 0 Using Vector - Declaration #include <vector> using namespace std; // vector of size 5 containing the integer value 0 vector<int> intVector(5); // vector of size 10; each element contains the empty string vector<string> strVector(10);

  9. Using Vector – Insertion/Removal Write code to remove {8} from a vector {12,-5,8,14,10}

  10. vector<int> v(5); 7 4 9 3 1 v.resize (10); 7 4 9 3 1 0 0 0 0 0 v.resize (4); 7 4 9 3 Using Vector – Resizing

  11. Application – Insertion Sort Start with Monroe Monroe Insert Chin in location 0; Monroe Chin Processing Chin Monroe moves to location 1 Insert Flores in location 1; Chin Flores Monroe Processing Flores Monroe moves to location 2 Chin Flores Monroe Stein Processing Stein Element Stein is OK Insert Dare at location 1; Processing Dare Chin Dare Flores Monroe Stein the tail of the list shifts to the right

  12. 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;

  13. 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;

  14. Insertion Sort - Code insertionSort(): template <typename T> void insertionSort(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; } }

  15. Example – Tracing the program a e d c b 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?

More Related