Part 6 - Templates, Containers, Vectors, Iterators, STL Algorithms Flashcards

1
Q

What does

template

above a method indicate?

A

T is a type parameter that can be instantiated with any type

So at compile time a version of the method will be generated for each type that uses it

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

Write a template version of the swap function

A

template

void swap(T& a, T& b) {

T tmp = a;

a = b;

b = tmp;

}

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

Write a method that prints a pair where each member of the pair can be of a different type

A

template

void printPair(const S& s, const T& t) {

cout << s << “, “ << t << endl;

}

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

What is a template class?

A

A class where one of the members can be of any type

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

Write the header for a class Pair that has two members of any type, a constructor and two methods getFirst and getSecond

A

template

class Pair {

private:

S first;

T second;

public:

Pair(S s, T t) : first(s), second(t){};

S getFirst() const { return first; }

T getSecond() const { return second; }

};

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

Why must a template class be written as one file?

A

Writing a separate file wont work because the type names S or T or whatever wont be in scope in the .cpp file. So has to be written in the header.

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

what is a container in STL?

A

A template class in the STL that contains a collection of objects and algorithms

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

What are the 3 sequence containers (that store sequential data) in the STL?

A

vector

deque

list

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

What are container adapters?

A

Containers that do not provide full functionality because their design means some operations may not apply

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

What are the 3 container adapters?

A

stack

queue

priority_queue

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

What are the 4 associative containers?

A

set

multiset

map

multimap

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

What does the no-arg constructor do for all STL containers?

copy constructor?

A

initialise the collection to be empty

initialise a collection to contain copies of the objects in the existing collection

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

What 4 functions do ALL classes in STL have? What do they do?

A

size(), empty(), max_size() and swap()

size() returns number of items in collection

empty() returns true if 0 items else false

max_size() returns max possible size for container

swap() takes reference to another container of same type and swaps the contents of the two containers

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

What does the clear() function do with containers?

A

Sets the collection to be empty

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

What does the vector class do? What header is needed?

A

Parametrised Array with range checking

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

what will vector v3(10, ‘x’); do?

A

Create a vector v3 that contains 10 x’s

17
Q

Why use .at over [] for vectors?

A

.at has range checking

18
Q

what does vector.reserve(100) do?

A

Increases the capacity of vector to 100

19
Q

If a vector is resize() ed to a smaller size than its current size what happens?

A

Elements after the resized size get deleted

20
Q

What are the 3 types of iterator?

A

unidirectional (++), bidirectional (++ and –) and random access (full poitner arithmetic)

21
Q

Write a function to iterate over a vector using a const iterator

A

void testFunc(const vector& v) {

vector::const_iterator it;

for (it = v.begin(); it != v.end(); it++) {

cout << *it << endl;

}

}

22
Q

Write a function to iterate over a vector backwards using a const iterator

A

void printBackwards(const vector& v) {

vector::const_reverse_iterator it;

for (it = v.rbegin(); it != v.rend(); it++) {

cout << *it << endl;

}

}

23
Q

How does the erase function work with STL containers?

A

takes a non const iterator and removes the item it is pointing to

24
Q

Write a function to remove the first instance of 0 in a vector

A

void removeFirst0(vector& v) {

vector::iterator it;

for (it = v.begin(); it != v.end(); it++) {

if (*it == 0) {v.erase(it); return;}

}

}

25
Q

Why can one iterator not be used in conjunction with erase() to remove more than one instance of a given thing?

A

The iterator value after erase() is called is undefined

26
Q

What does the two arg version of erase() do?

A

Removes all items in a range

e. g.
v. erase(v.begin(), v.begin+3); would remove the first 3 elements

27
Q

How do typedefs work?

A

typedef theactualtype alias;

e.g.

typedef vector::const_reverse_iterator CRI;

then you could use

CRI it = v.begin();

28
Q
A