F
F
Fortunato282018-06-09 14:47:27
C++ / C#
Fortunato28, 2018-06-09 14:47:27

How to correctly apply a function to all elements of a two-dimensional vector?

std::for_each(matrix.begin(), matrix.end(), [&](vector<Cell> cellLine)
    {
        std::for_each(cellLine.begin(), cellLine.end(), [&](Cell cell)
        {
            cell.showBody();
        });
    });

The above code compiles and works, iterates over each element of the matrix, as if calling the correct method of each cell, but the elements of the two-dimensional vector themselves do not change, although they should. I suspect that somewhere I do not understand something in lambdas. What's the matter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-06-09
@Fortunato28

something smart...

for(auto &cellLine:matrix)
{
    for(auto &cell:cellLine)
    {
        cell.showBody();
    }
}

foreach and lambda functions are obviously superfluous here. cppjunkie
already wrote about passing by reference

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question