Coding - practical 11 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a vector?

A

A vector is a special type of array that does not have a fixed size, instead elements can be continuously added and the vector will resize automatically.

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

How do you declare a vector type?

A

std::vector noteVector;

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

How do you add data to a vector?

A
  • use the class method push_back(), like so: noteVector.push_back(60);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens each time we call push_back()?

A

The supplied value gets added to end of the list.

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

How can you set the initial size of a vector?

A
- You can set the initial size of a vector using the class method resize and supplying a new size.
noteVector.resize(10);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you use an array vs vector?

A
Array:
- When you can determine the size 
- 'Put on the stack'
Vector:
- When you cannot determine the size 
- Want to pass data to a function
- 'Put on the heap'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sum up the three different data structures.

A
Array = fixed size, can only hold a single data type
Vector = variable size, can only hold a single data type
Class = can hold different data types
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Is it easier to pass vectors or arrays to functions?

A

Vectors as you can pass a vector of any size. With an array you need to specify the size of the array as the argument.

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

Why would we want to find the index of a certain value in an array?

A

Arrays can contain a large number of elements.

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

What is the general form of finding an algorithm (index an array)?

A
  • Create local variable set to -1
  • Iterate over each element in array/vector
  • If element matches value to look for
    (Set local variable to index)
  • If value is not found then -1 is returned
  • Otherwise the index will be the index of the
    value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you sort an array?

A

Simply use the std::sort function

e. g.
std: :sort (array.begin(), array.end());

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

What are array.begin and array.end?

A

These are called iterators

  • They tell the std::sort function what to sort
  • By default we want to sort all of the array
  • So from the beginning to the end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you copy the contents of an array to a vector?

A

We can again use the begin/end:

std::vector vectorFromArray (array.begin(), array.end());

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

What can be used to reverse an array?

A

std::reverse

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

What can be used to shuffle the contents of an array?

A

std::shuffle

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

std::vector is a template class, which means?

A

It requires you to supply the type of data the vector will
store within the triangle brackets. In this case the vector can only hold ints.
std::vector noteVector;