pointers, dynamic memory allocation, objects with dynamically allocated members, and vectors Flashcards

subjects for quiz 2

1
Q

vector

A

Stores elements like an array but can dynamically change in size. Adding and removing of elements are usually done at the end. Elements can be accessed by index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

stack

A

Stores elements in a specific order, called LIFO (Last In, First Out), where elements can only be added and removed from the top. Not accessible by index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

queue

A

Stores elements in a specific order, called FIFO (First In, First Out), where elements are added at the end and removed from the front. Not accessible by index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what do you need to use vectors?

A

include<vector></vector>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how do you declare a vector?

A

vector<type> vectorName;</type>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how do you declare a vector with 5 positions?

A

vector<type> vectorName (5);</type>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how do you declare vector Cars of type string with entries Volvo, BMW, and Dodge?

A

vector<string> Cars = {Volvo, BMW, Dodge};</string>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how do you write a for each loop?

A

for ( type of variable in vector variable name : vector name)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do you print out vector entries using a for each loop?

A

for( type of variable in vector variable name: vector name) { cout &laquo_space;variable name; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

iterators

A

used to access and iterate through elements of data structures (vectors, sets, etc.), by “pointing” to them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

algorithms

A

include functions, like sort() and find(), that perform operations on data structures through iterators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

can the type of vector (string, int, double, …) be changed after a vector has been declared?

A

no

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

how can you access vector elements using brackets?

A

vectorName[positionOfElement];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what does .front() do?

A

accesses the first element in the vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does .back() do?

A

accesses the last element in the vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what does .push_back() do?

A

adds an element at the end of the vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what does pop.back() do?

A

removes the last element from the vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

what function returns how many elements a vector has?

A

.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

how do you check if a vector is empty?

A

using the .empty() function included in the vector library

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what does .empty() return?

A

1 (true) if the vector is empty
0 (false) if the vector is not empty

21
Q

begin()

A

returns an iterator that points to the first element of the data structure

22
Q

end()

A

returns an iterator that points to one position after the last element

23
Q

when should you use an iterator over a for loop?

A

when you need to add, modify, or remove elements during iteration, iterate in reverse, or skip elements

24
Q

what do you need to be able to use sort() and find()?

A

include<algorithm></algorithm>

25
how do you use sort() to sort the entire vector?
sort(vectorName.begin(), vectorName.end());
26
how do you use find() to search an entire vector for the number 55?
find(vectorName.begin(), vectorName.end(), 55);
27
what does find() return?
an iterator pointing to the element
28
what are the two ways memory can be allocated?
at compile time or at run time
29
heap
It is the unused memory of the program and can be used to allocate the memory at runtime dynamically
30
what is the reference operator?
&
31
what is the dereference operator?
*
32
keyword for allocating memory
new
33
keyword for deallocating memory
delete
34
static variable / constant
memory location known at compile time
35
automatic variable
if we don't invoke the function the variable is located in, the memory will not be allocated
36
memory leak problem
when you reassign a pointer that already has a memory address (the program will get confused and not know which one to output)
37
int* ptr = new int; delete ptr; *ptr = 5; what is the issue with this code?
loose pointer problem, because we just deleted the memory address of ptr and then tried to assign a numerical value to a nonexistent memory location
38
int* p1; p1 = new int [10]; what is happening in the code above?
a pointer p1 is created, then it reserves 10 consecutive memory addresses (dynamically allocated array with 10 positions)
39
assume class myClass has two int and one int pointer variables. myClass obj = {1, 10, 2}; delete obj; what, if anything, is wrong with this code?
you should not use delete with a class object because it will not deallocate the dynamic memory in the class. In this example, it will not properly deallocate the int pointer variable.
40
how do you declare a copy constructor?
className(const className objectName&)
41
how should you copy objects with pointers?
by overloading the = operator
42
can you pass a vector with or without &?
yes
43
how do you declare an iterator?
vector::iterator iteratorName = .... (usually the first element of the vector)
44
how do you insert an element into a vector using an iterator?
vectorName.insert(iteratorName, thing to insert) --> (inserts the thing at the iterator's position)
45
how do you erase an element in a vector using an iterator?
vectorName.erase(iteratorName); --> erases the element at the iterators position
46
what do you need to be able to use iterators?
#include iterator
47
how do you overload the + operator?
className operator+ (const className variableName&)
48