C++: How to obtain the first elements of a matrix?

  c++

given that my matrix is defined as:

using matrix = std::vector<std::vector<double>>;

I’m trying to print the first element of each vector in this matrix.

std::vector<double> print_first_val(const matrix& all_val)

{
    std::vector<double> first_vals;

    long unsigned int size_of = all_val.size();

    for (unsigned int i = 0; i < size_of; i++)
    {
        int num = all_val[i][0];
        first_vals.push_back(num);
    }
    return first_vals;

int main()
{
    matrix input { { 1, 2, 2, 2, 3, 4 }, { -1, 1, 1.2, 2, 3.4, 4, 4 }, { 0, 3, 3, 4.5 } };

    auto output_2 = print_first_val(input);
    for (auto x : output_2) {
        std::cout << x << " ";
}

but for some reason, i’m getting the error:

conversion from ‘__gnu_cxx::__alloc_traits<std::allocator<double>, double>::value_type’ {aka ‘double’} to ‘int’ may change value [-Werror=float-conversion]

This warning happen at the line where int num = all_val[i][0]; is performed.

and I really have NO idea whats going on here, I’m quite new to c++ so it must be pretty obvious and I’m not getting it.

Source: Windows Questions C++

LEAVE A COMMENT