Chp 7 - Arrays and Vectors

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

Array

A variable that can store multiple values of the same type

  • values are stored in adjacent memory locations

  • declared using [ ] operator

    • int tests[5];

  • the size of an array is the total number of bytes allocated for it: (number of elements) * (size of each element)

2
New cards

Accessing Array Elements

each element in an array is assigned a unique subscript (starting at 0 to n - 1)

  • array elements can be used as regular variables

  • arrays must be accessed via individual elements

  • can access element with a constant or literal subscript

  • can use integer expression as subscript

<p>each element in an array is assigned a unique subscript (starting at 0 to n - 1)</p><ul><li><p>array elements can be used as regular variables</p></li><li><p>arrays must be accessed via individual elements</p></li><li><p>can access element with a constant or literal subscript</p></li><li><p>can use integer expression as subscript</p></li></ul><p></p>
3
New cards

Using a Loop to Step Through an Array

knowt flashcard image
4
New cards

Array Initialization

  • global array all elements initialized to 0 by default

  • local array all elements uninitialized by default

  • arrays can be initialized with an initialization list

    • int tests[5] = {79, 82, 91, 77, 84};

    • you can also determine the array size by the size of the initialization list

5
New cards

No Bounds Checking in C++

  • when you use a value as an array subscript, C++ does not check it to make sure it is a valid subscript

  • you can use subscripts beyond the bounds of the array, doing so can corrupt other memory locations, crash program, lock up computer, cause elusive bugs…BE CAREFUL!

6
New cards

The Range-Based for Loop

  • C++ 11

  • a loop that iterates once for each element in an array

  • each time the loop iterates, it copies an element from the array to a built-in variable, known as the range variable

  • the range-based for loop automatically knows the number of elements in an array, you do not have to:

    • use a counter variable

    • worry about stepping outside the bounds of the array

  • can be used in any situation where you do not need to use the element subscripts

<ul><li><p>C++ 11</p></li><li><p>a loop that iterates once for each element in an array</p></li><li><p>each time the loop iterates, it copies an element from the array to a built-in variable, known as the range variable</p></li><li><p>the range-based for loop automatically knows the number of elements in an array, you do not have to:</p><ul><li><p>use a counter variable</p></li><li><p>worry about stepping outside the bounds of the array</p></li></ul></li><li><p>can be used in any situation where you <strong><em>do not</em></strong> need to use the element subscripts</p></li></ul><p></p>
7
New cards

Copying one array to another

assign element-by-element

<p>assign element-by-element</p>
8
New cards

Printing an array

print element-by-element

<p>print element-by-element</p>
9
New cards

Structured Binding Declarations

defines a set of variables and initializes them with the values that are stored in an array

  • auto [variable1, variable2, etc…] = array;

<p>defines a set of variables and initializes them with the values that are stored in an array</p><ul><li><p><strong>auto [variable1, variable2, etc…] = array;</strong></p></li></ul><p></p>
10
New cards

Parallel Arrays

two or more arrays that contain related data

  • a subscript is used to relate arrays (elements at the same subscript are related)

  • arrays may be of different types

11
New cards

Arrays as Function Arguments

  • to pass an array to a function, use the array name

    • showScores(tests);

  • to define a function that takes an array parameter, use empty [] for array argument

    • void showScores(int []);

  • it is common to pass array size so that the function knows how many elements to process

    • showScores(tests, ARRAY_SIZE);

  • array size must be reflected in prototype header

12
New cards

Two-Dimensional Arrays

  • First declarator is number of rows, second is number of columns.

    • const int ROWS = 4, COLS = 3;

    • int exams[ROWS][COLS]

  • initialized row-by-row

  • some values in a row will be set to 0 or NULL

  • in a parameter

    • void getExams(int exams[][COLS], int rows)

<ul><li><p>First declarator is number of rows, second is number of columns.</p><ul><li><p><strong>const int ROWS = 4, COLS = 3;</strong></p></li><li><p><strong>int exams[ROWS][COLS]</strong></p></li></ul></li><li><p>initialized row-by-row</p></li><li><p>some values in a row will be set to 0 or NULL</p></li><li><p>in a parameter</p><ul><li><p><strong>void getExams(int exams[][COLS], int rows)</strong></p></li></ul></li></ul><p></p>
13
New cards

Arrays with 3+ Dimensions

  • you can define arrays with any number of dimensions

  • when used as a parameter, specify all but 1st dimension in prototype heading

    • void getRectSolid(short [][3][5])

14
New cards

STL vector

  • A data type defined in the Standard Template Library

  • can hold values of any type

  • automatically adds space as more is needed - no need to determine size at definition

  • can use [ ] to access elemets 

15
New cards

Declaring Vectors

  • #include<vector>

  • declare a vector to hold int element:

    • vector<int> scores;

  • declare a vector with initial size 30 (only starting size)

    • vector<int> scores(30);

  • declare a vector and initialize all elements to 0

    • vector<int> scores(30, 0);

  • declare a vector initialized to size and contents of another vector

    • vector<int> finals(scores);

16
New cards

adding elements to a vector

  • you can initialize a vector with a list of values (C++ 11)

    • vector<int> numbers {10, 20, 30, 40};

  • use push_back member function to add element to a full array or to an array that had no defined size:

    • scores.push_back(75);

  • use size member function to determine size of a vector

    • howbig = score.size();

17
New cards

removing vector elements

  • use pop_back member function to remove last element from vector

    • scores.pop_back();

  • to remove all contents of vector. use clear member function

    • scores.clear();

  • to determine if vector is empty, use empty member function

    • while (!scores.empty())

18
New cards

Range-Based for Loop with a vector

knowt flashcard image
19
New cards

Other Useful Member Functions

knowt flashcard image