Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer."— Presentation transcript:

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

2 Introduction to Data Structure, Fall 2006 Slide- 2 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).

3 Introduction to Data Structure, Fall 2006 Slide- 3 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.

4 Introduction to Data Structure, Fall 2006 Slide- 4 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.

5 Introduction to Data Structure, Fall 2006 Slide- 5 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?

6 Introduction to Data Structure, Fall 2006 Slide- 6 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

7 Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Example – Printing a vector template void writeVector(const vector & 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 Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Using Vector - Declaration #include using namespace std; // vector of size 5 containing the integer value 0 vector intVector(5); // vector of size 10; each element contains the empty string vector strVector(10); 0 4321 0 0 0 00

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

10 Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Using Vector – Resizing 7493 vector v(5); 1 749310000 v.resize(10); 0 749 v.resize(4); 3

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

12 Introduction to Data Structure, Fall 2006 Slide- 12 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;

13 Introduction to Data Structure, Fall 2006 Slide- 13 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;

14 Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno Insertion Sort - Code insertionSort(): template void insertionSort(vector & 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 Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno 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?


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

Similar presentations


Ads by Google