Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE202: Lecture 18The Ohio State University1 C++ Vector Class.

Similar presentations


Presentation on theme: "CSE202: Lecture 18The Ohio State University1 C++ Vector Class."— Presentation transcript:

1 CSE202: Lecture 18The Ohio State University1 C++ Vector Class

2 CSE202: Lecture 18The Ohio State University2 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!

3 CSE202: Lecture 18The Ohio State University3 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 To declare a vector, the C++ syntax is vector varName;

4 CSE202: Lecture 18The Ohio State University4 Declaring Vectors (1) Something you can do with arrays: –Declare a vector, v1[], with 10 integers: vector v1(10); –Declare a vector, v2[], with 25 characters: vector v2(25);

5 CSE202: Lecture 18The Ohio State University5 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 v2(n); –Automatically initialize all elements of a vector: // initialize vector elements to -1 vector v4(5, -1.0); –Return the number of elements in the vector: for (int i = 0; i < v2.size(); i++) { v2[i] = i*i; }

6 CSE202: Lecture 18The Ohio State University6 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; cout << "Enter number of integers: "; cin >> n; }

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

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

9 CSE202: Lecture 18The Ohio State University9 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;...

10 CSE202: Lecture 18The Ohio State University10... 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: 1 2 3 4 5 Reverse list: 5 4 3 2 1

11 CSE202: Lecture 18The Ohio State University11 reverse2.cpp... #include... int n(0); cout << "Enter length of list: "; cin >> n; vector 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;...

12 CSE202: Lecture 18The Ohio State University12... int n(0); cout << "Enter length of list: "; cin >> n; vector 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: 1 2 3 4 5 6 7 Reverse list: 7 6 5 4 3 2 1

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

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

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

16 CSE202: Lecture 18The Ohio State University16 Adding/Removing Elements

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

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

19 CSE202: Lecture 18The Ohio State University19... int x; vector list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { list.push_back(x); cin >> 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): 1 2 3 4 0 Reverse list: 4 3 2 1

20 CSE202: Lecture 18The Ohio State University20 reverseError.cpp... int x; vector 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. cin >> x; j++; } cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl;...

21 CSE202: Lecture 18The Ohio State University21 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. cin >> x; 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): 1 2 3 0 Segmentation fault

22 CSE202: Lecture 18The Ohio State University22 reverse4.cpp... #include... int main() { int x; vector 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 cin >> x; }...

23 CSE202: Lecture 18The Ohio State University23 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;...

24 CSE202: Lecture 18The Ohio State University24 Exercise int main() { vector 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; }

25 CSE202: Lecture 18The Ohio State University25 vector operations vector 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

26 CSE202: Lecture 18The Ohio State University26 Class vector as a Function Parameter

27 CSE202: Lecture 18The Ohio State University27 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]; // 5. max ← A[0]; } // 6. endif } // 7. endfor return(array_max); // 8. return max; }

28 CSE202: Lecture 18The Ohio State University28 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; }

29 CSE202: Lecture 18The Ohio State University29 Function findVectorMax() #include... int findVectorMax(const vector & 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); }

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

31 CSE202: Lecture 18The Ohio State University31 vectorMax1().cpp... cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { cin >> x; 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): 2 3 7 4 3 0 Maximum integer = 7

32 CSE202: Lecture 18The Ohio State University32 Function moveMax() #include... 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]); }

33 CSE202: Lecture 18The Ohio State University33 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; }

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

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

36 CSE202: Lecture 18The Ohio State University36 reverseError2().cpp // function prototypes void reverse(vector v); void print_list(const char label[], const vector v); int main() { vector 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; }

37 CSE202: Lecture 18The Ohio State University37 reverseError2().cpp // reverse list void reverse(vector 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 v) { cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; }

38 CSE202: Lecture 18The Ohio State University38 reverseError2Debug().cpp // reverse list void reverse(vector 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); }

39 CSE202: Lecture 18The Ohio State University39 reverse5().cpp // function prototypes void reverse(vector & v); void print_list(const char label[], const vector & v); int main() { vector 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; }

40 CSE202: Lecture 18The Ohio State University40 reverse5().cpp // reverse list void reverse(vector & 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 & v) { cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl; }

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

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

43 CSE202: Lecture 18The Ohio State University43 sortInt().cpp #include... int main() { int x; vector list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { cin >> x; 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;...

44 CSE202: Lecture 18The Ohio State University44 vectorMax3().cpp #include... int main() { int x; vector list; cout << "Enter list of positive integers (ending in 0): "; cin >> x; while (x > 0) { cin >> x; 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; }...

45 CSE202: Lecture 18The Ohio State University45 sortString().cpp #include... int main() { string s; vector list; cout << "Enter list of strings (ending with the string \".\"): "; cin >> s; while (s != ".") { cin >> 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;...

46 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: CSE202: Lecture 18The Ohio State University46

47 CSE202: Lecture 18The Ohio State University47 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.


Download ppt "CSE202: Lecture 18The Ohio State University1 C++ Vector Class."

Similar presentations


Ads by Google