Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Ohio State University

Similar presentations


Presentation on theme: "The Ohio State University"— Presentation transcript:

1 The Ohio State University
C++ Vector Class CSE1222: Lecture 18 The Ohio State University

2 The Ohio State University
Limitations of Arrays The size of arrays must be a known constant prior to compile time. const MAX_SIZE(1000); double A[MAX_SIZE]; Inefficient use of space e.g. A program needs enough storage to maintain 1000 real numbers, but on average, the user only enters 10 real elements. So, 900 * 8 bytes = 7200 bytes wasted on average. Arrays cannot be resized in your program What if a user needs 2000 elements? Then a program with which has an array of size 1000 is useless! CSE1222: Lecture 18 The Ohio State University

3 The Ohio State University
Vector Class The C++ vector class is an alternative to using regular arrays, and helps us avoid array limitations. To use vectors, we must #include <vector> To declare a vector, the C++ syntax is vector<dataType> varName; CSE1222: Lecture 18 The Ohio State University

4 The Ohio State University
Declaring Vectors (1) Something you can do with arrays: Declare a vector, v1[], with 10 integers: vector<int> v1(10); Declare a vector, v2[], with 25 characters: vector<char> v2(25); CSE1222: Lecture 18 The Ohio State University

5 The Ohio State University
Declaring Vectors (2) Vectors can do the following: Declare the size of a vector using a variable cout << “How many strings need to be stored?”; cin >> n; vector<strings> v2(n); Automatically initialize all elements of a vector: // initialize vector elements to -1 vector<double> v4(5, -1.0); Return the number of elements in the vector: for (int i = 0; i < v2.size(); i++) { v2[i] = i*i; } CSE1222: Lecture 18 The Ohio State University

6 The Ohio State University
logTable.cpp ... const int SIZE(10); double log_table[SIZE]; // array of SIZE elements int n; cout << "Enter number of integers: "; cin >> n; while (n > SIZE) { cout << "Input error.“; cout << " Input must be less than or equal to " << SIZE << "." << endl; } CSE1222: Lecture 18 The Ohio State University

7 The Ohio State University
logTable.cpp (cont.) for (int i = 0; i < n; i++) { log_table[i] = log(double(i+1)); } cout << "log(" << i+1 << ") = " << log_table[i] << endl; return 0; CSE1222: Lecture 18 The Ohio State University

8 The Ohio State University
logTable2.cpp ... #include <vector> int n(0); cout << "Enter number of integers: "; cin >> n; vector<double> log_table(n); // Note: Must have (n) for (int i = 0; i < log_table.size(); i++) { log_table[i] = log(double(i+1)); } { cout << "log(" << i+1 << ") = " << log_table[i] << endl; } CSE1222: Lecture 18 The Ohio State University

9 The Ohio State University
reverse.cpp ... const int ARRAY_SIZE(5); int list[ARRAY_SIZE]; // an array of ARRAY_SIZE elements cout << "Enter list of " << ARRAY_SIZE << " integers: "; for (int i = 0; i < ARRAY_SIZE; i++) { cin >> list[i]; } cout << "Reverse list: "; for (int i = ARRAY_SIZE-1; i >= 0; i--) cout << list[i] << " "; cout << endl; CSE1222: Lecture 18 The Ohio State University

10 The Ohio State University
... const int ARRAY_SIZE(5); int list[ARRAY_SIZE]; // an array of ARRAY_SIZE elements cout << "Enter list of " << ARRAY_SIZE << " integers: "; for (int i = 0; i < ARRAY_SIZE; i++) { cin >> list[i]; } cout << "Reverse list: "; for (int i = ARRAY_SIZE-1; i >= 0; i--) cout << list[i] << " "; cout << endl; > reverse.exe Enter list of 5 integers: Reverse list: CSE1222: Lecture 18 The Ohio State University

11 The Ohio State University
reverse2.cpp ... #include <vector> int n(0); cout << "Enter length of list: "; cin >> n; vector<int> list(n); cout << "Enter list of " << list.size() << " integers: "; for (int i = 0; i < list.size(); i++) { cin >> list[i]; } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; CSE1222: Lecture 18 The Ohio State University

12 The Ohio State University
... int n(0); cout << "Enter length of list: "; cin >> n; vector<int> list(n); cout << "Enter list of " << list.size() << " integers: "; for (int i = 0; i < list.size(); i++) { cin >> list[i]; } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; > reverse2.exe Enter length of list: 7 Enter list of 7 integers: Reverse list: CSE1222: Lecture 18 The Ohio State University

13 The Ohio State University
logTable2.cpp ... #include <vector> int n(0); cout << "Enter number of integers: "; cin >> n; vector<double> log_table(n); // Note: Must have (n) for (int i = 0; i < log_table.size(); i++) { log_table[i] = log(double(i+1)); } { cout << "log(" << i+1 << ") = " << log_table[i] << endl; } CSE1222: Lecture 18 The Ohio State University

14 The Ohio State University
logTableError.cpp ... #include <vector> int n(0); cout << "Enter number of integers: "; cin >> n; vector<double> log_table; // Missing (n) for (int i = 0; i < log_table.size(); i++) { log_table[i] = log(double(i+1)); } { cout << "log(" << i+1 << ") = " << log_table[i] << endl; } CSE1222: Lecture 18 The Ohio State University

15 The Ohio State University
logTableError2.cpp ... #include <vector> int n(0); cout << "Enter number of integers: "; cin >> n; vector<double> log_table; // Missing (n) for (int i = 0; i < n; i++) { log_table[i] = log(double(i+1)); } { cout << "log(" << i+1 << ") = " << log_table[i] << endl; } CSE1222: Lecture 18 The Ohio State University

16 Adding/Removing Elements
CSE1222: Lecture 18 The Ohio State University

17 Reverse List of Positive Integers
Read in input list of positive integers ending in 0; Output list in reverse order. CSE1222: Lecture 18 The Ohio State University

18 The Ohio State University
reverse3.cpp ... int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; CSE1222: Lecture 18 The Ohio State University

19 The Ohio State University
... int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; > reverse3.exe Enter list of positive integers (ending in 0): Reverse list: CSE1222: Lecture 18 The Ohio State University

20 The Ohio State University
reverseError.cpp ... int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; int j = 0; while (x > 0) { list[j] = x; // ERROR. Memory for list[j] is not allocated. j++; } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; CSE1222: Lecture 18 The Ohio State University

21 The Ohio State University
reverseError.cpp ... cout << "Enter list of positive integers (ending in 0): "; cin >> x; int j = 0; while (x > 0) { list[j] = x; // ERROR. Memory for list[j] is not allocated. j++; } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl; > reverseError.exe Enter list of positive integers (ending in 0): Segmentation fault CSE1222: Lecture 18 The Ohio State University

22 The Ohio State University
reverse4.cpp ... #include <vector> int main() { int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) list.push_back(x); // add element x to back of list } CSE1222: Lecture 18 The Ohio State University

23 The Ohio State University
reverse4.cpp (cont) ... cout << "Reverse list: "; while (list.size() > 0) { int n = list.size(); x = list[n-1]; cout << x << " "; list.pop_back(); // remove last element from list } cout << endl; CSE1222: Lecture 18 The Ohio State University

24 The Ohio State University
Exercise int main() { vector<int> v; for (int i = 3; i < 12; i++) v.push_back(2*i); } cout << v[2] << endl; cout << v[3] << endl; cout << v[4] << endl; cout << v.size() << endl; return 0; CSE1222: Lecture 18 The Ohio State University

25 The Ohio State University
vector operations vector<int> list; Some member functions of class vector: list[3] = 5; // access element int x = list[3]; list.size(); // number of elements list.push_back(5); // add element to back list.pop_back(); // remove last element list.clear(); // remove all elements CSE1222: Lecture 18 The Ohio State University

26 Class vector as a Function Parameter
CSE1222: Lecture 18 The Ohio State University

27 The Ohio State University
Function findMax2() int findMax2(const int array[], const int array_size) { int array_max(0); if (array_size < 1) { return(0); }; // 1. Error: return 0. array_max = array[0]; // 2. max ← A[0]; for (int i = 1; i < array_size; i++) // 3. for i=1 to size-1 do if (array[i] > array_max) // 4. if (A[i]>max) then array_max = array[i]; // max ← A[0]; } // 6. endif } // 7. endfor return(array_max); // 8. return max; } CSE1222: Lecture 18 The Ohio State University

28 The Ohio State University
arrayMax3().cpp . . . // function findMax2() prototype int findMax2(const int array[], const int array_size); int main() // main function { const int SIZE_A(6); int array_A[SIZE_A] = { 5, 3, 1, 7, 3, 5 }; const int SIZE_B(8); int array_B[SIZE_B] = { 2, 6, 9, 0, 6, 4, 5, 1 }; int Amax(0), Bmax(0); Amax = findMax2(array_A, SIZE_A); Bmax = findMax2(array_B, SIZE_B); cout << "Max of array A = " << Amax << endl; cout << "Max of array B = " << Bmax << endl; return 0; } CSE1222: Lecture 18 The Ohio State University

29 Function findVectorMax()
#include <vector> ... int findVectorMax(const vector<int> & v) { int vmax(0); if (v.size() < 1) { return(0); }; // Error: return 0. int vmax = v[0]; for (int i = 1; i < v.size(); i++) if (v[i] > vmax) vmax = v[i]; } return(vmax); CSE1222: Lecture 18 The Ohio State University

30 The Ohio State University
vectorMax1().cpp #include <vector> ... int findVectorMax(const vector<int> & v); int x(0), vmax(0); vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); // add element x to back of list }; if (list.size() > 0) { vmax = findVectorMax(list); cout << "Maximum integer = " << vmax << endl; } CSE1222: Lecture 18 The Ohio State University

31 The Ohio State University
vectorMax1().cpp ... cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); // add element x to back of list }; if (list.size() > 0) { vmax = findMax(list); cout << "Maximum integer = " << vmax << endl; } > vectorMax1.exe Enter list of positive integers (ending in 0): Maximum integer = 7 CSE1222: Lecture 18 The Ohio State University

32 The Ohio State University
Function moveMax() #include <algorithm> ... void moveMax(int array[], const int size) // Note: size is still const { for (int i = 1; i < size; i++) if (array[0] < array[i]) swap(array[0], array[i]); } CSE1222: Lecture 18 The Ohio State University

33 The Ohio State University
arrayMax4().cpp // include & namespace statements . . . // function moveMax() prototype void moveMax(int array[], const int size); int main() // main function { const int SIZE_A(6); int array_A[SIZE_A] = { 5, 3, 1, 7, 3, 5 }; const int SIZE_B(8); int array_B[SIZE_B] = { 2, 6, 9, 0, 6, 4, 5, 1 }; moveMax(array_A, SIZE_A); moveMax(array_B, SIZE_B); cout << "Max of array A = " << array1[0] << endl; cout << "Max of array B = " << array2[0] << endl; return 0; } CSE1222: Lecture 18 The Ohio State University

34 Function move VectorMax()
#include <vector> ... void moveVectorMax(vector<int> & v) { for (int i = 1; i < v.size(); i++) if (v[i] > v[0]) swap(v[i], v[0]); } CSE1222: Lecture 18 The Ohio State University

35 The Ohio State University
vectorMax2().cpp #include <vector> ... void moveVectorMax(vector<int> & v); int x(0); vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); // add element x to back of list }; if (list.size() > 0) { moveVectorMax(list); cout << "Maximum integer = " << list[0] << endl; } CSE1222: Lecture 18 The Ohio State University

36 The Ohio State University
reverseError2().cpp // function prototypes void reverse(vector<int> v); void print_list(const char label[], const vector<int> v); int main() { vector<int> list; list.push_back(11); list.push_back(12); list.push_back(13); print_list("List: ", list); reverse(list); print_list("Reverse list: ", list); return 0; } CSE1222: Lecture 18 The Ohio State University

37 The Ohio State University
reverseError2().cpp // reverse list void reverse(vector<int> v) { int n = v.size(); for (int i = 0; i < n/2; i++) swap(v[i], v[n-1-i]); } // print list void print_list(const char label[], const vector<int> v) cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; CSE1222: Lecture 18 The Ohio State University

38 The Ohio State University
reverseError2Debug().cpp // reverse list void reverse(vector<int> v) { int n = v.size(); for (int i = 0; i < n/2; i++) swap(v[i], v[n-1-i]); } print_list("Function reverse: ", v); CSE1222: Lecture 18 The Ohio State University

39 The Ohio State University
reverse5().cpp // function prototypes void reverse(vector<int> & v); void print_list(const char label[], const vector<int> & v); int main() { vector<int> list; list.push_back(11); list.push_back(12); list.push_back(13); print_list("List: ", list); reverse(list); print_list("Reverse list: ", list); return 0; } CSE1222: Lecture 18 The Ohio State University

40 The Ohio State University
reverse5().cpp // reverse list void reverse(vector<int> & v) { int n = v.size(); for (int i = 0; i < n/2; i++) swap(v[i], v[n-1-i]); } // print list void print_list(const char label[], const vector<int> & v) cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; CSE1222: Lecture 18 The Ohio State University

41 The Ohio State University
Pass by Reference Class vector should always be passed by reference; Use const to indicate a vector which is not going to be modified. CSE1222: Lecture 18 The Ohio State University

42 The Ohio State University
Function sort The standard template library sort function will sort the elements of a vector in increasing order; #include <algorithm> #include <vector> // sort from the beginning to the end of the list sort(list.begin(), list.end()); CSE1222: Lecture 18 The Ohio State University

43 The Ohio State University
sortInt().cpp #include <algorithm> #include <vector> ... int main() { int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); // add element x to back of list } sort(list.begin(), list.end()); for (int i = 0; i < list.size(); i++) { cout << list[i] << " "; cout << endl; CSE1222: Lecture 18 The Ohio State University

44 The Ohio State University
vectorMax3().cpp #include <algorithm> #include <vector> ... int main() { int x; vector<int> list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); // add element x to back of list } sort(list.begin(), list.end()); if (list.size() > 0) int k = list.size()-1; cout << "Maximum integer = " << list[k] << endl; CSE1222: Lecture 18 The Ohio State University

45 The Ohio State University
sortString().cpp #include <algorithm> #include <vector> #include <string> ... int main() { string s; vector<string> list; cout << "Enter list of strings (ending with the string \".\"): "; cin >> s; while (s != ".") { list.push_back(s); // add element s to back of list } sort(list.begin(), list.end()); for (int i = 0; i < list.size(); i++) { cout << list[i] << " "; cout << endl; CSE1222: Lecture 18 The Ohio State University

46 The Ohio State University
Warning Do not confuse C++ vectors with vectors in mathematics/geometry; C++ vectors store information in a 1-dimensional array; A geometric vector is a geometric object which has both length and direction: CSE1222: Lecture 18 The Ohio State University

47 The Ohio State University
So, Why Use Arrays at All?! Overhead Vectors use more space and (sometimes) take more time than arrays. When should arrays be used over vectors? When the array has fixed length. When the dataset does not need to be contracted or expanded. When the array is 2 (or 3 or 4) dimensional When speed is an issue. Note: C++ vectors are implemented using C arrays. CSE1222: Lecture 18 The Ohio State University


Download ppt "The Ohio State University"

Similar presentations


Ads by Google