chapter 8egabrielsen/cs1342/chapter8.pdf · 2020. 2. 26. · chapter 8 stl arrays / vectors. arrays...
TRANSCRIPT
Chapter 8STL Arrays / Vectors
Arrays
Arrays are a sequence of elements that are contained in a contiguous block of memory.
Arrays do NOT know their own size. User is responsible for managing an array’s size.
Regular arrays of local scope are left uninitialized - contents of the array are unknown.
example: arrays.cpp
const int ARRAY_SIZE = 10; int myArray[ARRAY_SIZE];
myArray
Array Initialization
const int ARRAY_SIZE = 5;
int myArray[ARRAY_SIZE]; myArray[0] = 1; myArray[1] = 2; …
// With C++11
int myArray[ARRAY_SIZE] = { 1, 2, 3, 4, 5 };
int myArray[ARRAY_SIZE] { 1, 2, 3, 4, 5 };
int myArray[ARRAY_SIZE] { };
example: arrays.cpp
Arrays in functions
example: arrays_func.cpp
Arrays are inherently passed by reference.
Any updates made to myArray are maintained through the rest of the program.
void printArray(int myArray[]);
Vectors
A vector is a sequence container representing arrays that can change in size
• Just like arrays, vectors use contiguous storage for elements
• Each item in a vector is called an element
• Use vectors with #include <vector>
Vector Declaration
A vector must always be declared with a data type.
If declared with no numElements, the vector will be a default size of 0.
vector<dataType> vectorName(numElements);
vector<dataType> vectorName;
Vector Initialization
Vectors by default automatically initialize all elements to 0.
We can specify the default value by passing in a second param to the constructor.
vector<int> myVector(4);
0 0 0 0myVector
vector<int> myVector(4, -1);
-1 -1 -1 -1myVector
Vector Access
Vectors by default automatically initialize all elements to 0.
Use the .at( index ) function to access an element in the vector
Using .at() is typically safer than using [ ] syntax.
vector<int> myVector;
example: vector_access.cpp
Access Errors
A common error is to access an index of a vector that is out of range of the vector’s index range.
Vector’s throw an exception when accessing an index out of bounds.
Arrays do not throw an error but rather return whatever was in that space in memory.
example: bad_access.cpp
Iterating through vectors
Iterating through vectors is the same as iterating through arrays since both are from 0 to N.
• .size() - returns the size of the vector. Can use this when iterating through the loop rather than explicitly keeping track of its size
example: findMax.cpp
// Iterating through myVector for (i = 0; i < myVector.size(); ++i) { // Loop body accessing myVector.at(i) }
Vector push_back
• .push_back(element) is used to append an element to the end of a vector
• First creates a new element at the end of the vector
• Second assigns the element with the value passed into push_back
example: vector_push_back.cpp
Vector resize
• Vectors can dynamically change in size
• A vector’s size can be changed during runtime by using the resize(newSize) function.
• If new size is larger then the current vector then space is allocated at the end of the vector
• If the new size is smaller then vector removes elements off the end of the vector
example: vector_resize.cpp
Other Vector Functions
Parallel Arrays/Vectors
Often times we want to associate different pieces of data together.
Ex: A program that acts as a phone book
vector<string> names; vector<string> phoneNumbers;
name.at(0); phoneNumbers.at(0);
example: prices.cpp
Comparing and Copying Vectors
Unlike arrays, we can use the assignment operator and the comparison operator in c++ to assign and compare vectors.
vector<int> vector1 = {1, 2, 3}; vector<int> vector2 = {1, 2};
(vector1 == vector2) // false
vector2 = vector1; // copy vector1 to vector2
(vector1 == vector2) // true
example: prices.cpp
Reversing a Vector
Write a function that reverses a vector using swap.
First think about how we swap variables:
int x = 10; int y = 20;
int temp = x; x = y; y = temp;
Reversing a Vector
Now we apply this to reversing a vector.
vector<int> integers = {1, 2, 3, 4, 5, 6};
example: reverse.cpp
Arrays vs Vectors
Arrays: int myList[10]; myList[i];
Vectors: vector<int> myList(10); myList.at(i);
Vectors can grow and change In size dynamically in an efficient way.
Arrays are fixed in size.
Arrays are passed by reference. Vectors can be passed by value or by reference.
Two Dimensional Arrays
2D Arrays can be used to represent a table of information rather than an a singular array or row.
Initializes a 2D array with r # of rows and c # of columns
int table[r][c];
int table[2][3];
table
0 1 2
0
1
Initializing 2D arrays
First Method:
Second Method:
int x[3][4] = {0,1,2,3,4,5,6,7,9,10,11};
int x[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };
example: multi_dimensional_arrays.cpp
Exercise
1. x[1][2] =
2. x[0][3] =
3. x[1][5] =
4. x[3][2] =
int x[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };
Exercise
1. x[1][2] =
2. x[0][3] =
3. x[1][5] =
4. x[3][2] =
int x[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };
6
3
9
unknown
Multi-Dimensional Vectors
Just like multi-dimensional arrays, we can use multi-dimensional vectors to represent table data.
vector<vector<int>> vect;
examples: 2d_vector.cpp2d_vector_size.cpp
Multi-Dimensional Arrays
We can also represent n-dimensional arrays in c++, provided we have enough memory.
Size of the array: i * j * k.
int multiArray[i][j][k];
Multi-Dimensional Arrays
from: https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/