I read here and also here and examples on cpluplus.com And I still don't understand how it works
what confuses me at most how lambdas work with _if algorithms like copy_if that they don't reference containers in the body
std::vector<int> foo = {25,15,5,-5,-15};
std::vector<int> bar (foo.size());
// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i)
{return !(i<0);} )
doesn't reference vector object foo in the body. so how it performs the desired action?
Additionally, I don't understand what is the difference beetween capturing a variable and passing as parameter
I also tried my own example:
vector<unsigned long>v2(10);
for(unsigned long i=0;i<v2.size();i++)
v2[i]=10-i;
v2.erase(remove_if(v1.begin(), v1.end(),[](unsigned long n) {
return n%2==1; } ),v2.end());//remove odd numbers
It compiles (MVS 2010 with Intel Composer 14) but produces rubbish and assertion error.