1 / 70

Vectors continued

Briana B. Morrison Adapted from Alan Eugenio. Vectors continued. Topics. Vectors Implementation Applications InsertionSort High Precision Arithmetic. CLASS vector. <vector>. Operations. iterator begin ();

flynn-silva
Download Presentation

Vectors continued

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. Briana B. Morrison Adapted from Alan Eugenio Vectorscontinued

  2. Vectors Topics Vectors Implementation Applications InsertionSort High Precision Arithmetic

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

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

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

  6. 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() );

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

  8. 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 );

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

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

  11. 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:

  12. Vectors Input: apple banana candy donut egg

  13. Vectors Application using Vectors Insertion Sort

  14. Vectors The Insertion Sort

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

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

  17. 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; } }

  18. Vectors APPLICATION HIGH-PRECISION ARITHMETIC

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

  20. Vectors

  21. Vectors

  22. Vectors

  23. Vectors

  24. Vectors

  25. Vectors

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

  27. Vectors

  28. Vectors

  29. Vectors

  30. Vectors

  31. Vectors

  32. Vectors

  33. Vectors

  34. Vectors

  35. Vectors

  36. Vectors

  37. Vectors

  38. Vectors

  39. 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’

  40. Vectors

  41. Vectors

  42. Vectors

  43. Vectors

  44. Vectors

  45. Vectors

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

  47. Vectors

  48. Vectors

  49. Vectors

  50. Vectors

More Related