Question: . Scrutinise the code shown in Figure Q 3 on page 7 . The numbers at the beginning of each line are provided for ease
Scrutinise the code shown in Figure Q on page The numbers at the beginning of
each line are provided for ease of reference and are not part of the program.
a The operators and & have different meanings depending on whether they are
used in a declaration or an expression. Using diagrams, explain the meaning of
used in the each of the following lines:
int ip new int ;
ip ;
b Using English words, write out the full type of the variable vp which is declared
on line of Figure Q
c Using English words, write out the full type of the arguments of the template
function zipvectors declared on line
d Explain why the unique pointers themselves cannot be passed as arguments,
but must be passed by reference.
e Explain what the function zipvectors does. Include the types of the arguments
and return value. Hence deduce the programs expected output.
f the author of the function made a great effort to avoid passing vectors as
arguments. Explain why.
g The author made no effort to avoid returning a vector from the function. Explain
why.
h How could the type of the arguments be changed to avoid the use of pointers
but without compromise in function, safety or efficiency?
When line is changed to set vp to and the program is recompiled,
the following output is produced:
terminate called after throwing an instance of
std::outofrange'
what: vector::Mrangecheck:
n which is thissizewhich is
Aborted core dumped
i Why did the change result in an exception being thrown?
j Decide on a desired behaviour when the arguments are vectors of differing length,
and write out a revised version of zipvectors incorporating the improvements
you suggested in Qh
Page of OVER
#include
#include
#include
template
std::vector zipvectors
std::uniqueptr const& vptr
std::uniqueptr const& vptr
std::vector result;
result.reservevptrsize;
for int i ; i vptrsize ; i
result.pushbackvptrati;
result.pushbackvptrati;
return result;
int main
auto vp std::makeunique;
vp;
auto vp std::makeunique;
vp;
for const int& i : zipvectorsvp vp
std::cout i t;
std::cout std::endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
