chapter 8egabrielsen/cs1342/chapter8.pdf · 2020. 2. 26. · chapter 8 stl arrays / vectors. arrays...

25
Chapter 8 STL Arrays / Vectors

Upload: others

Post on 28-Mar-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

Chapter 8STL Arrays / Vectors

Page 2: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 3: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 4: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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[]);

Page 5: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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>

Page 6: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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;

Page 7: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 8: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 9: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 10: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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) }

Page 11: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 12: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 13: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

Other Vector Functions

Page 14: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 15: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 16: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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;

Page 17: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

Reversing a Vector

Now we apply this to reversing a vector.

vector<int> integers = {1, 2, 3, 4, 5, 6};

example: reverse.cpp

Page 18: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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.

Page 19: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 20: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 21: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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} };

Page 22: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 23: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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

Page 24: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

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];

Page 25: Chapter 8egabrielsen/cs1342/Chapter8.pdf · 2020. 2. 26. · Chapter 8 STL Arrays / Vectors. Arrays Arrays are a sequence of elements that are contained in a contiguous block of memory

Multi-Dimensional Arrays

from: https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/