I have a problem with my function template.
I have 3 different collections and I've got iterators for the collections. Now I have to create an function template 'apply' which will do the following: 1. Pass through all elements of collections and check if predicate is true:
1.1 If predicate return true - then element of collection need to be changed with lambda 'passed'
1.2 if predicate return false = then element of collection need to be changed with lambda 'rejected'
Please give me an example how should I write it.
Thank you soo much for help. Updated code here:
#include <iostream>
#include <vector>
#include <list>
#include <functional>
using namespace std;
template<typename T>
void apply(T collBegin, T collEnd, function<bool(int)> f1, function<int(int)> f2, function<int(int)> f3)
{
for (T actualPosition = collBegin; actualPosition != collEnd; ++actualPosition) {
if (f1(*actualPosition)) {
//the argument matches the predicate function f1
*actualPosition = f2(*actualPosition);
}
else {
//the argument doesn't match the predicate function f1
*actualPosition = f3(*actualPosition);
}
}
}
int main()
{
int arr[]{ 1,2,3,4,5,6,7,8,9 };
auto predicate = [](int arg) -> bool { return arg % 2 == 0; };
auto passed = [](int arg) -> int { return arg / 2; };
auto rejected = [](int arg) -> int { return (3 * arg) + 1; };
apply(arr, arr + std::size(arr), predicate, passed, rejected);
std::vector<int> vec(arr, arr + std::size(arr));
apply(vec.begin(), vec.end(), predicate, passed, rejected);
std::list<int> lis(vec.begin(), vec.end());
apply(lis.begin(), lis.end(), predicate, passed, rejected);
for (auto e : lis) std::cout << e << " ";
std::cout << '\n';
}
This code works. But I want to make change it from int to T. How can I do this ?
std::functiontypes to be the same. There's your problem. Also, vector iterators are not plain pointers, so attempting to use them asT *is not going to work, either.std::apply. It might not have happened yet, but you might end up accidentally calling it, or at least being confused when it comes up in error messages. This is why you should not writeusing namespace std;where it will apply to everything.