Final Flashcards

1
Q

Implement a template class definition for pair

A
template
 class Pair
 {
 public:
 Pair(const F& a, const S& b);
 F get_first() const;
 S get_second() const;

private:
F first;
S second;
};

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

Implement the constructor for the template pair function (type F and S)

A
template
Pair::Pair(const F& a, const S& b)
{
 first = a;
 second = b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

implement the get_first function for the template pair function (type F and S)

A
template
F Pair::get_first() const
{
 return first;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
In this example CMP may be either a \_\_\_\_\_ or a \_\_\_\_\_\_\_\_
template
T max(const T& left, const T& right, CMP cmp)
{
 if (cmp(left, right))
 return right;
 return left;
}
A

Actual function or function object

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

what is the syntax for a template max function comparing employee objects alice & fred with functor CompareBySalary

A

max(alice, fred, CompareBySalary());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the error here?
OrderedCollection a;
OrderedCollection b;
void f(OrderedCollection& c);
f(a);
f(b);
A

f(a); // Error - types don’t match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • When designing functions , order parameters such that
  • those _____ likely to use a default appear first
  • those ___likely to have a default value appear last.
A
  • When designing functions , order parameters such that
  • those least likely to use a default appear first
  • those most likely to have a default value appear last.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the header of a class OrderedCollection using the Less functor?

A
template >
class OrderedCollection
{
 ...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the advantage of defining a make_pair function t as a template class?

A

We can execute a line like this auto pair1 = make_pair(5, “red”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
\_\_\_\_\_ template is a template function or class that can take a
varying number of parameters
A

Variadic template

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

Variadic templates use varying parameters also known as _______ _____

A

parameter pack

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

Implement a print function that can take any number of inputs

A

void display(){}
template
void display(const Tfirst& param1, const Trest&…
params)
{std::cout <
void print(const Tvals& … parameters){
display(parameters…);}

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