Generate binary image using coordinate vectors I have a matrix M of size [n_rows, n_cols] and a list of x and y pixel coordinates. I use these coordinates to draw a binary image. Here is my code:
std::vector&lt;std::vector&lt;double&gt;&gt; x;
std::vector&lt;std::vector&lt;double&gt;&gt; y;

// fill x and y

std::vector&lt;std::vector&lt;unsigned int&gt;&gt; M(n_rows, std::vector&lt;unsigned int&gt;(n_cols));

std::vector&lt;std::vector&lt;double&gt;&gt;::const_iterator iter_row_x = x.begin();
std::vector&lt;std::vector&lt;double&gt;&gt;::const_iterator iter_row_y = y.begin();

for (; iter_row_x != x.end(); ++iter_row_x, ++iter_row_y) {
    std::vector&lt;double&gt;::const_iterator iter_col_x = iter_row_x[0].begin();
    std::vector&lt;double&gt;::const_iterator iter_col_y = iter_row_y[0].begin();
    for (; iter_col_x != iter_row_x[0].end(); ++iter_col_x, ++iter_col_y) {
        unsigned int i = unsigned int(*iter_col_x);
        unsigned int j = unsigned int(*iter_col_y);
        M[i][j] = 1;
    }
}

I would like to know if there is a more elegant way to achieve this? My code above looks a bit bulky and error-prone for such a simple task.
auto
Automatic type deduction is especially welcoming for iterators, as their types are bulky. We can rewrite to
std::vector&lt;std::vector&lt;double&gt;&gt; x;
std::vector&lt;std::vector&lt;double&gt;&gt; y;

// fill x and y

std::vector&lt;std::vector&lt;unsigned int&gt;&gt; M(n_rows, std::vector&lt;unsigned int&gt;(n_cols));

auto iter_row_x = x.cbegin(); //note cbegin to get const_iterator
auto iter_row_y = y.cbegin();

for (; iter_row_x != x.end(); ++iter_row_x, ++iter_row_y) {
    auto iter_col_x = iter_row_x-&gt;cbegin(); // iter_row_x[0].cbegin() = iter_row_x-&gt;cbegin()
    auto iter_col_y = iter_row_y-&gt;cbegin();
    for (; iter_col_x != iter_row_x[0].end(); ++iter_col_x, ++iter_col_y) {
        auto i = static_cast&lt;unsigned int&gt;(*iter_col_x); // be explicit about which cast you are using. Now you do not need to specify the type twice.
        auto j = static_cast&lt;unsigned int&gt;(*iter_col_y);
        M[i][j] = 1;
    }
}

But it's still wordy. Having separate vectors for x and y seems unnecessary, since it is expected that they should be of same length. Better create a structure for xy and create only one vector.
struct Point 
{
    double x;
    double y;
}

Then we have
std::vector&lt;std::vector&lt;Point&gt;&gt; xy;

// fill xy

std::vector&lt;std::vector&lt;unsigned int&gt;&gt; M(n_rows, std::vector&lt;unsigned int&gt;(n_cols));

for (auto iter_row_xy = xy.cbegin() ; iter_row_xy != xy.end(); ++iter_row_xy) {
    for (auto iter_col_xy = iter_row_xy-&gt;cbegin(); iter_col_x != iter_row_x[0].end(); ++iter_col_x, ++iter_col_y) {
        auto i = static_cast&lt;unsigned int&gt;(*iter_col_xy.x);
        auto j = static_cast&lt;unsigned int&gt;(*iter_col_xy.y);
        M[i][j] = 1;
    }
}

But we can do even better by using for range:
std::vector&lt;std::vector&lt;Point&gt;&gt; xy;

// fill xy

std::vector&lt;std::vector&lt;unsigned int&gt;&gt; M(n_rows, std::vector&lt;unsigned int&gt;(n_cols));

for (const auto&amp; elements : xy){
    for (const auto&amp; point : elements){
        auto i = static_cast&lt;unsigned int&gt;(point.x);
        auto j = static_cast&lt;unsigned int&gt;(point.y);
        M[i][j] = 1;
    }
}