Q:

How to return a 2d vector in C++ which all of its elements are 1

I am new to C++ and I want to create a 2d vector which all of its elements are 1. When I run my code the compiler returns nothing!
#include <iostream>
#include <vector>

using namespace std;

vector<vector<double>> ones2d(int m, int n) {
    vector<vector<double>> vect;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++){
            vect[i][j] = 1;
        }
    }
    return vect;
}

int main(){
    vector<vector<double>> vect = ones2d(2,3);
    cout << vect[0][0] << endl;
}enter code here

A:

The resize() function is used to resize a vector to the specified size. We can use it to initialize a 2d vector with given default value :
std::vector<std::vector<double>> ones2d(int m, int n)
{
    const int default_value = 1;

    std::vector<std::vector<double>> vect(m);
    for (int i = 0; i < m; i++)
        vect[i].resize(n, default_value);

    return vect;
}

Or you can use of fill constructor to initialize a 2d vector :
std::vector<std::vector<double>> ones2d_2(int m, int n)
{
    const int default_value = 1;

    return std::vector<std::vector<double>>(m, std::vector<double>(n, default_value));
}

I recommend use of the second solution.
Please try it online

