Search code examples
c++c++11lambdastdinstdvector

Vector passed by reference inside lambda cannot be modified even if mutable keyword is used


I am trying to populate a vector with integer values which are coming in from standard input as follows:

std::vector<int> v;

for_each(v.begin(),v.end(),([&](auto &i) mutable {cin>>i; v.push_back(i);}));

However, this statement is not working and the vector does not get filled with incoming values. Can you please suggest where I am going wrong? Thanks


Solution

  • std::for_each applies the function object (the lambda) on each element in the passed container. Therefore to the parameter passed to the lambda is the current elements in the vector however, your vector is empty so there's nothing to iterate over so it never runs. You are also trying to assign the value from std::cin to the element in the vector i and then push a reference to that element again to the back of the vector along with the already existing element.

    So first of all, if you want to use std::for_each you need to pre-allocate the vectors memory so it has something to iterate over.

    std::vector<int> v(5); ///< allocates space for 5 ints

    You then can then run std::for_each and std::cin into the lambda's input parameter which is a iterator to the vector, saving the inputs to the vector.

    std::vector<int> v(5);
    
    std::for_each(v.begin(), v.end(), [](auto& i) { std::cin >> i; });
    

    The benefit of this solution is that it will automatically terminate reading values once it has finished iterating through the vector, only reading the desired amount of values.

    Links and Resources