Homework 3 Fall
Programming Part:
1. Write a function templatethat called print if :
• takes three parameters: two iterators start, end, and a functor pred start and end have the capabilities of a forward iterator, and refer to a range
[start,end) in a container
pred is a functor that takes an element in the range [start,end) as an arguement and returns a bool
• prints1 all items in the range [start,end) which evaluates to true
• runs in O(n) time where n is the number of items in the range [start,end) The signature of your function template is:
template void print_if( Itr start, Itr end, UnaryPred pred )
A small amount of extra credit will be given for figuring out how to add your own test case to the unit test for this assignment.
Written Part
1. For the vector class, and for the following code snippet:
vector c { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
vector::iterator itr1 = c.begin()+2; vector::iterator itr2 = c.begin()+4; vector::iterator itr3 = c.begin()+8; cout << *(c.begin( ) + ( c.end( ) – c.begin( ) )/2 ) << endl;
c.erase(itr2);
cout << *itr1 << endl; cout << *itr2 << endl; cout << *itr3 << endl;
cout << *(c.begin( ) + ( c.end( ) – c.begin( ) )/2 ) << endl;
What is printed? Explain your answer.3
2. Using big-Oh notation, give the worst case run time for the method print if, which you implemented programming problem 1.
3. Given the following code snippet:
vector a {1,2,3,4, …, n}; // vector, a, has n items vector::iterator itrStart; vector::iterator itrMid; vector::iterator itrEnd;
Assign values to the iterators, itrStart, itrMid, itrEnd, so that:
(a) [itrStart, itrMid) refers to the range 1,2, 3, …, n/2
(b) [itrMid, itrEnd) refers to the range n/2+1,n/2+2, …, n
4. For each code snippet below state either why the code won’t compile/run, or state what isprinted by the code snippet.
(a) vector a {1, 2, 3, 4, 5}; vector::iterator itra = a.begin(); cout << *(itra + 3);
(b) list b {1, 2, 3, 4, 5}; list::iterator itrb = b.begin(); cout << *(itrb + 3);
(c) list c {1, 2, 3, 4, 5}; list::iterator itrc = c.end(); itrc–; cout << *(itrc);
(d) vector d {1, 2, 3, 4, 5}; vector::iterator itrd = d.begin(); cout << *(itrd + 3);
[SOLVED] CS2134-Homework 3- Write a function template called print if
40.00 $
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
Securely Powered by:
- Homework-3-dtcll4.zip



