Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.

Similar presentations


Presentation on theme: "1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm."— Presentation transcript:

1 1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm

2 2 Final Exam next week Wednesday Feb 4, 7:30 pm – 9:30 pm Our usual room Exam will be comprehensive. In addition to questions seen previously, be sure to study Lab 9: Ch 7 ex. 4 Lab 10: Ch 9 ex. 2, 3, 4 Lab 11: Ch 10 ex 11, 12, 13 (Skip Ch10 ex 16, 17) ☞ One page of notes (both sides) allowed ☜

3 3 An array is a list of values (all of the same type) Each value is named by an index (a.k.a. subscript) Subscripts numbered 0, 1, …. Are passed by reference, not value. Arrays in C++ int a[5], b[3] = {5, 5, 5}; type variable[size]; type variable[size] = {value, value, … } Name specific elements with variable[index expression]; cin >> a[k+2]; b[i] = b[j] + 2;

4 4 The initial size of an array can vary at runtime. But once you allocate an array, you can't change its size. Arrays are Fixed-Size int len; cin >> len; int a[len]; // (Runtime error if len < 0) len = len+1; // (Doesn't change size of a)

5 5 A Vector Is a Resizable ArrayNeed to #include The size member function returns current number of elements in vector Vectors in C++ vector a; // size 0 vector b(5); // size 5 vector c(5,8); // size 5; 5 copies of 8 cout << c.size(); // size 5 Declarations: vector variable; vector variable(size); vector variable(size,value);

6 6 Another Vector Declaration #include using namespace std; int main() { vector x; vector y(5); vector z(5,"hello"); return(0); }

7 7 Can inspect/update elements using square bracket notation with indexCan assign vector1 = vector2 (makes fresh copy of vector2) Using Vectors vector b(5), c(5,8); c[0] = 10; c[1] = c[0]*2; // c[1] = 20 a = c; // make copy of c a[1] = 30; cout << c[1]; // Still 20

8 8 Example: Accessing Vector Components #include using namespace std; int main() { vector x; vector y(4); vector z(4,"hello"); y[0] = 6; cin >> z[2]; cout << z[2].length() << endl; // string length 5 return(0); }

9 The resize Member Function In addition to size, vectors have other member functions. One of them is resize. vectorVar.resize(newSize) changes the size of the vector. We usually make vectors larger. But we can shorten them (toss rightmost elements as necessary). Resizing a long vector can take a long time. Example: vector y(4); … y.resize(y.size()*2);

10 The push_back Member Function vectorVar.push_back(value) adds the value to the right end of the vector. (New largest subscript.) Easy way to build a vector by repeatedly adding elements. Example: vector y; double newVal; cin >> newVal; while (newVal > 0) { y.push_back(newVal); cin >> newVal; }

11 Passing Vectors to Functions When passing vectors to functions Pass-by-Value is the default Not like pass-by-reference for arrays Passing a vector by value causes a copy of the vector to be made. Can take lots of time for large vector Can pass vectors by reference for better efficiency.

12 12 Passing Vectors to Functions #include using namespace std; void pbv(vector x); void pbr(vector &x); int main() { vector v(5); v[0] = 10; pbv(v); cout << v[0] << endl; pbr(v); cout << v[0] << endl; return(0); } void pbv(vector x) { x[0] = 1000; cout << x[0] << endl; } void pbr(vector &x) { x[0] = 1000; cout << x[0] << endl; }


Download ppt "1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm."

Similar presentations


Ads by Google