MOD 2_ZYBOOKS 2_STRUCTS Flashcards
(25 cards)
The ____ construct defines a new type, which can be used to declare a variable with subitems.
Struct
VIEW: A struct enables creating a variable with data members.
How do you define a new struct type?
Each struct subitem is called a ____ ____. For a declared variable, each struct data member can be accessed using “.”, known as a ____ ____ operator
data member, member access
What happens when you assign a variable of a struct type to another such variable?
Assigning a variable of a struct type to another such variable automatically assigns each corresponding data member.
Can a struct be used to return multiple values?
Yes.
VIEW: Using a struct that is returned from a function; the struct’s data members are copied upon return.
T/F: A variable of a struct type CANNOT be a function parameter.
False, it can.
And just like other types, a pass by value parameter would copy the item, while a pass by reference parameter would not.
A ____ is an ordered list of items of a given data type.
Vector
Each item in a vector is called an ____.
Element
A programmer must include the statement #include ____ at the top of the file when planning to use vectors.
<vector>
</vector>
How do you declare a vector?
vector<dataType> vectorName(numElements);</dataType>
Terminology note: { } are ____. < > are ____ ____.
Braces, angle brackets
In a vector access, the number in .at() parentheses is called the ____ of the corresponding element.
Index
VIEW: A vector declaration creates multiple variables in memory, each accessible using .at()
A powerful aspect of vectors is that the ____ is an expression.
(Give an example)
Index
Ex: userNums.at(i) uses the value held in the int variable i as the index. As such, a vector is useful to easily lookup the Nth item in a list.
A vector’s index must be what type?
It cannot be what type?
Unsigned integer type
Cannot be a floating-point type.
VIEW: Vector’s ith element can be directly accessed using .at(i - 1): Oldest people program.
A key advantage of vectors becomes evident when used in conjunction with ____.
Loops
A vector’s ____ function returns the number of vector elements.
size()
VIEW: Vectors combined with loops are powerful together: User-entered numbers.
A vector’s elements are automatically initialized to ____s during the vector declaration.
0
Can a vector’s elements be initialized to another single value besides 0?
Give an example.
Yes
Ex: vector<int> myVector(3, -1); creates a vector named myVector with three elements, each with value -1.</int>
How can you initialize each vector element with different values?
A programmer may initialize each vector element with different values by specifying the initial values in braces {} separated by commas.
Ex: vector<int> carSales = {5, 7, 11}; creates a vector of three integer elements initialized with values 5, 7, and 11.</int>
Such vector declaration and initialization does not require specifying the vector size, because the vector’s size is automatically set to the number of elements within the braces. For a larger vector, initialization may be done by first declaring the vector, and then using a loop to assign vector elements.