Chapter 8 Arrays, Strings and pointers 8c: Vector (array)
Chapter Goals: To learn basic concept of vector To learn how to access vector To grow or shrink vector To apply vector using function To develop vector algorithm
8.1 Concepts the size of the array has to be known when the program is compiled. A vector collects a sequence of values, just like an array does, but its size can change (more convenient than partial filled array) A vector expands to hold as many elements as needed
8.2 Define Example:
Use bracket to access vector element Just like you do with array Size member function to obtain current size of vector
8.2 Access Vector push_back Example 1: Example 2: add element to the end of vector, increasing size by 1. Example 1: Example 2:
8.2 Access Vector pop_back Example: Removes the last element of vector, shrinking size by 1. Example:
8.3 Vector & Function Vectors can occur as function arguments and return values. Use a reference parameter to modify the contents of a vector. a function can return a vector
8.4 Vector Algorithm Refer textbook p288.
Exercise 35. vector <int> primeNum; primeNum.push_back(2); 36. vector <int> primeNum(5); primeNum[0] = 2; primeNum[1] = 3; primeNum[2] = 5; primeNum[3] = 7; primeNum[4] = 11; 37. Ann Cal
Exercise