Modify the body of the four functions in the file helpers.h
used by the
main.cpp
file. In addition to the explanations below you will find in main.cpp
different use cases with input and the expected result
IMPORTANT :
- you CANNOT use any STL algorithm unless explicitly allowed.
- The only files you are allowed to modify are
main.cpp
andhelpers.h
For your convenience below are links to the STL definitions
- find and find_if: https://en.cppreference.com/w/cpp/algorithm/find
- remove and remove_if: https://en.cppreference.com/w/cpp/algorithm/remove
- reverse: https://en.cppreference.com/w/cpp/algorithm/reverse
Implement the function
template<typename Iter,typename T>
Iter find(Iter begin, Iter end, const T& val);
It should take a range from begin to end and finds the first occurrence of val. It returns an iterator to the first occurrence.
Implement the function
template<typename Iter, typename P>
Iter find_if(Iter begin, Iter end, P pred);
It should take a range from begin to end and finds the first element for which (function or function object) pred returns true, i.e. the first element e such that pred(e)
returns true. It returns an iterator to the first such occurrence.
Implement the function
template <typename Iter,typename T>
Iter remove(Iter begin,Iter end, T val);
It should "remove" all occurrences of val. It returns an iterator to the end of the "useful" values. Let itr be the returned iterator then the range [begin,itr)
does not contain val anymore. The range [itr,end)
may or may not contain val. Note that the order of the elements in the range [begin,itr)
should be the same as the original. To understand more the effect of this function, let v be a vector then the code below removes all occurrence of the value val from the vector while retaining the original order of the elements.
auto itr=remove(v.begin(),v.end(),val);
v.erase(itr,end);
Implement the function
template<typename Iter,typename P>
Iter remove_if(Iter begin,Iter end, P pred);
The effect of this function is the same as remove() in part 3 except that it "removes" all the elements e for which pred(e)
returns true.
Implement the function
template <typename Iter>
void reverse(Iter begin,Iter end);
Implement a function similar to reverse
, call it nc_reverse
, that works with non-copyable types.
You can test your implementation by reversing a vector containing two std::unique_ptr<int>
elements.
Define a class struct NoCopy;
that has no member variables and is not copyable.