SOFT30161 Advanced Software Engineering
Lab
tasks
week
2
&
3
Templates
and
overloading
Predict which library functions are called when the following code executes
std::cout << result << <<std::string(is ) << (11/10/11) << std::endl;
Check what VC does.
You should overload the stream insertion operator << to provide formatted
output of the class std::vector<int>
What is the prototype of this function1?
Use [ ] to bracket the elements; separate the elements with a comma
followed by a space, but do not have leading or trailing , or so an empty
vector should be only 2 characters.
In all cases aim for clear efficient2 code
Level 1 define your function explicitly for std::vector<int>
Level 2 use templates so the function will work with vectors of any3 element
type
Level 3 generalize your definition to any Container not just vectors; could
you have different brackets for the different containers, but still use this
approach?
Some questions
Does your solution work for nested vectors?
Do the stream manipulators4 affect the way your code behaves?
Would you put this definition in a namespace if so, what would you call it?
1 It will vary with Level
2
No wasted tests
3
Its not really any what restrictions are there on the element type?
4
Such as std::ostream::setw
Richard Hibberd 88356 - v2013.1
SOFT30161 Advanced Software Engineering
The
SE
BST
-
exercises
1. complete the BST the unimplemented parts are guarded with
assertions, so find out how they work
2. check == (it doesnt work fix it)
3. use a local reference as an alias to improve the readability of your code
(at least one use!) The example is not that good, because if leftChild
was not a reference, the tree just wont build (leftChild is updated by
the recursive call, but goes out of scope without affecting current).
This version is (half-)coded in the project; use leftChild & rightChild in
the recursive calls & ry it on the first 3 months.
template <class TE>
void BST<TE>::insSub(BSTnode* & current, const TE & element)
{
if (current == nullptr)
current = new BSTnode(nullptr, element, nullptr);
else
{
BSTnode* & leftChild = current->left;
BSTnode* & rightChild = current->right;
if (element < current->elem)
insSub(leftChild, element);
else
insSub(rightChild, element);
}
}
implement the BSTnodePtr reference approach to searching
Richard Hibberd 88356 - v2013.1