I'm relatively new to C++ and I can't seem to phrase my question correctly since vectorize and map both have meanings which seem different from what I am looking for (so Googling for a solution is difficult).
I have many functions acting on single objects of arbitrary type. I want a general way (i.e. a template or wrapper function) to convert these to functions acting on vectors of the arbitrary number of objects of the same type. An example:
int func1(int a) {return a+1;}
long func2(long a) {return a*10;}
vector<int> func1(vector<int> a_vec)
{
vector<int> out_vec;
for (int i = 0; i < a_vec.size(); i++)
out_vec.push_back(func1(a_vec[i]));
return out_vec;
}
vector<long> func2(vector<long> a_vec)
{
vector<long> out_vec;
for (int i = 0; i < a_vec.size(); i++)
out_vec.push_back(func2(a_vec[i]));
return out_vec;
}
Templates seem to be necessary but I don't have much experience with them yet and have no idea how to apply them in this case. Any references/suggestions/comments welcome. (Also please add relevant tags - since I'm not sure of the correct term for this I'm not sure how to tag it).
Aucun commentaire:
Enregistrer un commentaire