C++ Arrays and Vectors
Arrays
After we ask user for n, we will ask user to input these n numbers. Since we need to go over the numbers many times, we need to store them somewhere. How do we store all these n numbers?
int num[n];
Array declaration is just like variable declarations, except you specify how many in a square brackets.
Now we can do an input loop and store each number in sequence, such as:
for (int i=0; i<n; i++) cin >> num[i];
What if I want to store many records like excel spread sheet?
C-style, pointer based, Array
C++ style, object based, Vector
Declare an array
Initialize an array
static versus dynamic
what's the difference?
when do we use each kind?
tradeoff and consequence?
Pass an array as a function argument
need to also pass the size of the array
basically array name is just an address to the beginning of the array, see pointer page for example.
Array operations
search, insert, delete, sort (typical data manipulation functions)
to keep track of an array, we need to know where it is (address, aka array name), the capacity (max size), and length.
Vectors
Instantiate a vector object from template library
what type, how many elements
Initialize a vector
can vector size be changed dynamically?
What are the vector class public functions?
http://www.cplusplus.com/reference/stl/vector/
push_back, size, operator[], operator=, empty
Pass a vector object as a function argument
NO need to also pass the size of the vector
Search through the vector
Insert, delete, sort (typical data manipulation functions)
push_back, pop_back, assign
Two dimensional vector (matrix)
So, why do we even bother talking about array?
-----------