I have a method which basically updates each value of the passed reference array by 5.
Initially I tried the below options but it wouldn't update the reference array.
Option 1:
void Int_Array_Update(std::vector<int>& param)
{
auto f1 = [](int n) { return n + 5; };
std::for_each(param.begin(), param.end(), f1);
}
Option 2:
void Int_Array_Update(std::vector<int>& param)
{
auto f1 = [&](int n) { return n + 5; };
std::for_each(param.begin(), param.end(), f1);
}
Option 3:
void Int_Array_Update(std::vector<int>& param)
{
auto f1 = [¶m](int n) { return n + 5; };
std::for_each(param.begin(), param.end(), f1);
}
The workaround I have currently is as below:
void Int_Array_Update(std::vector<int>& param)
{
for (int i = 0; i < param.size(); ++i)
param[i] = param[i] + 5;
}
I am not able to find any answers why the lamda function is not updating the reference vector or maybe I am doing something wrong. Any help is appreciated.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire