cse202: lecture 18the ohio state university1 c++ vector class

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

Upload: jordan-davis

Post on 21-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

CSE202: Lecture 18 The Ohio State University 1

C++ Vector Class

CSE202: Lecture 18 The Ohio State University 2

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!

CSE202: Lecture 18 The Ohio State University 3

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 isvector<dataType> varName;

CSE202: Lecture 18 The Ohio State University 4

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

CSE202: Lecture 18 The Ohio State University 5

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 -1vector<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; }

CSE202: Lecture 18 The Ohio State University 6

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

CSE202: Lecture 18 The Ohio State University 7

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

CSE202: Lecture 18 The Ohio State University 8

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)); }

for (int i = 0; i < log_table.size(); i++) { cout << "log(" << i+1 << ") = " << log_table[i] << endl; }...

CSE202: Lecture 18 The Ohio State University 9

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

CSE202: Lecture 18 The Ohio State University 10

... 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.exeEnter list of 5 integers: 1 2 3 4 5Reverse list: 5 4 3 2 1

CSE202: Lecture 18 The Ohio State University 11

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;

...

CSE202: Lecture 18 The Ohio State University 12

... 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.exeEnter length of list: 7Enter list of 7 integers: 1 2 3 4 5 6 7Reverse list: 7 6 5 4 3 2 1

CSE202: Lecture 18 The Ohio State University 13

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)); }

for (int i = 0; i < log_table.size(); i++) { cout << "log(" << i+1 << ") = " << log_table[i] << endl; }...

CSE202: Lecture 18 The Ohio State University 14

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)); }

for (int i = 0; i < log_table.size(); i++) { cout << "log(" << i+1 << ") = " << log_table[i] << endl; }...

CSE202: Lecture 18 The Ohio State University 15

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)); }

for (int i = 0; i < n; i++) { cout << "log(" << i+1 << ") = " << log_table[i] << endl; }...

CSE202: Lecture 18 The Ohio State University 16

Adding/Removing Elements

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

CSE202: Lecture 18 The Ohio State University 18

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); cin >> x; }

cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl;...

CSE202: Lecture 18 The Ohio State University 19

... int x; vector<int> 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.exeEnter list of positive integers (ending in 0): 1 2 3 4 0Reverse list: 4 3 2 1

CSE202: Lecture 18 The Ohio State University 20

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. cin >> x; j++; }

cout << "Reverse list: "; for (int i = list.size(); i > 0; i--) { cout << list[i-1] << " "; } cout << endl;...

CSE202: Lecture 18 The Ohio State University 21

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.exeEnter list of positive integers (ending in 0): 1 2 3 0Segmentation fault

CSE202: Lecture 18 The Ohio State University 22

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 cin >> x; }...

CSE202: Lecture 18 The Ohio State University 23

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

CSE202: Lecture 18 The Ohio State University 24

Exerciseint 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;

}

CSE202: Lecture 18 The Ohio State University 25

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

CSE202: Lecture 18 The Ohio State University 26

Class vector as a Function Parameter

CSE202: Lecture 18 The Ohio State University 27

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;

}

CSE202: Lecture 18 The Ohio State University 28

arrayMax3().cpp. . .// function findMax2() prototypeint 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;}

CSE202: 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);}

CSE202: Lecture 18 The Ohio State University 30

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) { 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; }...

CSE202: Lecture 18 The Ohio State University 31

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.exeEnter list of positive integers (ending in 0): 2 3 7 4 3 0Maximum integer = 7

CSE202: Lecture 18 The Ohio State University 32

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]); } } }

CSE202: Lecture 18 The Ohio State University 33

arrayMax4().cpp// include & namespace statements. . .// function moveMax() prototypevoid 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;}

CSE202: 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]); } }}

CSE202: Lecture 18 The Ohio State University 35

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) { 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; }...

CSE202: Lecture 18 The Ohio State University 36

reverseError2().cpp// function prototypesvoid 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;}

CSE202: Lecture 18 The Ohio State University 37

reverseError2().cpp// reverse listvoid reverse(vector<int> v){ int n = v.size(); for (int i = 0; i < n/2; i++) { swap(v[i], v[n-1-i]); }}

// print listvoid print_list(const char label[], const vector<int> v){ cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl;}

CSE202: Lecture 18 The Ohio State University 38

reverseError2Debug().cpp// reverse listvoid 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);}

CSE202: Lecture 18 The Ohio State University 39

reverse5().cpp// function prototypesvoid 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;}

CSE202: Lecture 18 The Ohio State University 40

reverse5().cpp// reverse listvoid reverse(vector<int> & v){ int n = v.size(); for (int i = 0; i < n/2; i++) { swap(v[i], v[n-1-i]); }}

// print listvoid print_list(const char label[], const vector<int> & v){ cout << label; for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl;}

CSE202: Lecture 18 The Ohio State University 41

Pass by Reference

• Class vector should always be passed by reference;

• Use const to indicate a vector which is not going to be modified.

CSE202: Lecture 18 The Ohio State University 42

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

CSE202: Lecture 18 The Ohio State University 43

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

CSE202: Lecture 18 The Ohio State University 44

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) { 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; }...

CSE202: Lecture 18 The Ohio State University 45

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 != ".") { 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;...

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 18 The Ohio State University 46

CSE202: Lecture 18 The Ohio State University 47

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.