Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Arrays, Strings and pointers

Similar presentations


Presentation on theme: "Chapter 8 Arrays, Strings and pointers"— Presentation transcript:

1 Chapter 8 Arrays, Strings and pointers
8c: Vector (array)

2 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

3 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

4 8.2 Define Example:

5 Use bracket to access vector element
Just like you do with array Size member function to obtain current size of vector

6 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:

7 8.2 Access Vector pop_back Example:
Removes the last element of vector, shrinking size by 1. Example:

8 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

9 8.4 Vector Algorithm Refer textbook p288.

10 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

11 Exercise


Download ppt "Chapter 8 Arrays, Strings and pointers"

Similar presentations


Ads by Google