700 likes | 836 Views
Briana B. Morrison Adapted from Alan Eugenio. Vectors continued. Topics. Vectors Implementation Applications InsertionSort High Precision Arithmetic. CLASS vector. <vector>. Operations. iterator begin ();
E N D
Briana B. Morrison Adapted from Alan Eugenio Vectorscontinued
Vectors Topics Vectors Implementation Applications InsertionSort High Precision Arithmetic
Vectors CLASS vector <vector> Operations iterator begin(); Returns an iterator that references the first position (0) of the list. If the vector is empty, the iterator value end() is returned. iterator end(); Returns an iterator that signifies a location immediately out of the range of actual elements. A program must not dereference the value of end() with the * operator.
Vectors Using Iterators vector<int> v; for (int i = 1; i <= 100; i++ ) v.push_back(i); vector<int>::iterator vectorIter; vectorIter = v.begin(); //initialize vectorIter = v.end(); //past end of list
Vectors CLASS vector <vector> Operations void erase(iterator pos); Erase the element pointed to by pos. Precondition: The vector is not empty. Postcondition: The vector has one fewer element. void erase(iterator first, iterator last); Erase all list elements within the iterator range [first, last]. Precondition: The vector is not empty. Postcondition: The vector decreases by the number of elements in the range.
Vectors Using Iterators vector<int> v; for (int i = 1; i <= 100; i++ ) v.push_back(i); vector<int>::iterator vectorIter; v.erase ( v.begin() ); v.erase ( v.end() ); // ERROR v.erase( v.begin(), v.end() );
Vectors CLASS vector <vector> Operations iterator insert(iterator pos, const T& value); Insert value before pos, and return an iterator pointing to the position of the new value in the vector. The operation does not affect any existing iterators. Postcondition:The vector has a new element.
Vectors Using Iterators vector<int> v; for (int i = 1; i <= 100; i++ ) v.push_back(i); vector<int>::iterator vectorIter; v.insert ( v.begin(), 255 ); v.insert ( v.end(), 777 );
Vectors CLASS vector::iterator <vector> Operations * : Accesses the value of the item currently pointed to by the iterator. *iter; ++ : Moves the iterator to the next item in the vector. iter++; -- : Moves the iterator to the previous item in the vector. iter--; == : Takes two iterators as operands and returns true when they both point at the same item in the vector. iter1 == iter2 != : Returns true when the two iterators do not point at the same item in the vector. iter1 != iter2
Vectors Using Iterators vector<int> v; for (int i = 1; i <= 100; i++ ) v.push_back(i); vector<int>::iterator vectorIter; vectorIter = v.end(); //past end of list vectorIter--; //last element cout << *vectorIter; if (vectorIter != v.begin() ) vectorIter = v.begin(); if (*vectorIter == *v.end() ) // error! cout << “Same elements.” << endl;
Vectors Program Example Here is a program fragment that reads in several words, one per line, deletes the first and last words read in, and prints out the rest:
Vectors Input: apple banana candy donut egg
Vectors Application using Vectors Insertion Sort
Vectors The Insertion Sort
Vectors Insertion Sort Algorithm insertionSort(): // sort a vector of type T using insertion // sort template <classname T> void insertionSort(vector<T>& v) { int i, j, n = v.size(); T temp;
Vectors Insertion Sort Algorithm // place v[i] into the sublist v[0] ... v[i-1] // 1 <= i < n, so it’s in the correct position for (i = 1; i < n; i++) {// index j scans down list from v[i] // looking for correct position to locate target. // assign current value to v[j] j = i; temp = v[i]; // locate insertion point by scanning // downward as long as temp < v[j-1] // and we have not encountered the // beginning of the list
Vectors Insertion Sort Algorithm while (j > 0 && temp < v[j-1]) { // shift elements up list to make // room for insertion v[j] = v[j-1]; j--; } // the location is found; insert temp v[j] = temp; } }
Vectors APPLICATION HIGH-PRECISION ARITHMETIC
Vectors IN PUBLIC-KEY CRYPTOGRAPHY, THE INTEGERS ARE HUNDREDS OF DIGITS LONG. There is no data type that we can use to store integers that are this long. We must create one.
Vectors Design Time Now that we know the “what”, We have to figure out the “how”: How do we store the information in computer memory? What happens with each operation? What will memory look like?
Vectors For example, suppose the calling very_long_int object has the value 13579. then least (0) RETURNS ‘9’ least (1) RETURNS ‘7’ least (2) RETURNS ‘5’ least (3) RETURNS ‘3’ least (4) RETURNS ‘1’ least (5) RETURNS ‘0’ least (6) RETURNS ‘0’
Vectors Design of Vector The following describes one possible implementation (Hewlett-Packard) of the vector class. The underlying storage facility is an array: that’s how random-access is achieved.