简体   繁体   中英

Initializing a two dimensional std::vector

So, I have the following:

std::vector< std::vector <int> > fog;

and I am initializing it very naively like:

    for(int i=0; i<A_NUMBER; i++)
    {
            std::vector <int> fogRow;
            for(int j=0; j<OTHER_NUMBER; j++)
            {
                 fogRow.push_back( 0 );
            }
            fog.push_back(fogRow);
    }

And it feels very wrong... Is there another way of initializing a vector like this?

Use the std::vector::vector(count, value) constructor that accepts an initial size and a default value:

std::vector<std::vector<int> > fog(
    ROW_COUNT,
    std::vector<int>(COLUMN_COUNT)); // Defaults to zero initial value

If a value other than zero, say 4 for example, was required to be the default then:

std::vector<std::vector<int> > fog(
    ROW_COUNT,
    std::vector<int>(COLUMN_COUNT, 4));

I should also mention uniform initialization was introduced in C++11, which permits the initialization of vector , and other containers, using {} :

std::vector<std::vector<int> > fog { { 1, 1, 1 },
                                    { 2, 2, 2 } };
                           

Let's say you want to initialize 2D vector, m*n, with initial value to be 0

we could do this

#include<iostream>
int main(){ 
    int m = 2, n = 5;

    vector<vector<int>> vec(m, vector<int> (n, 0));

    return 0;
}

std::vector没有append方法,但如果你想制作一个包含A_NUMBERint向量的向量,每个向量都包含other_number零,那么你可以这样做:

std::vector<std::vector<int>> fog(A_NUMBER, std::vector<int>(OTHER_NUMBER));

The general syntax, as depicted already is:

std::vector<std::vector<int> > v (A_NUMBER, std::vector <int> (OTHER_NUMBER, DEFAULT_VALUE))  

Here, the vector 'v' can be visualised as a two dimensional array, with 'A_NUMBER' of rows, with 'OTHER_NUMBER' of columns with their initial value set to 'DEFAULT_VALUE'.

Also it can be written like this:

std::vector <int> line(OTHER_NUMBER, DEFAULT_VALUE)
std::vector<std::vector<int> > v(A_NUMBER, line)

Inputting values in a 2-D vector is similar to inputting values in a 2-D array:

for(int i = 0; i < A_NUMBER; i++) {
     for(int j = 0; j < OTHER_NUMBER; j++) {
         std::cin >> v[i][j]
     }
}

Examples have already been stated in other answers....!

The recommended approach is to use fill constructor to initialize a two-dimensional vector with a given default value :

std::vector<std::vector<int>> fog(M, std::vector<int>(N, default_value));

where, M and N are dimensions for your 2D vector.

I think the easiest way to make it done is :

std::vector<std::vector<int>>v(10,std::vector<int>(11,100));

10 is the size of the outer or global vector, which is the main one, and 11 is the size of inner vector of type int, and initial values are initialized to 100! That's my first help on stack, i think it helps someone.

Suppose you want to initialize a two dimensional integer vector with n rows and m column each having value ' VAL '

Write it as

std::vector<vector<int>> arr(n, vector<int>(m,VAL));

This VAL can be a integer type variable or constant such as 100

这是对我有用的:

vector<vector<int>> minA{ROW_SIZE, vector<int>(COLUMN_SIZE, VALUE)};

This answer will help to easily initialize 2d vector after declaration.

int n=4,m=3;
int default_value = 0;

std::vector<std::vector<int>> matrix;

matrix.resize(m, std::vector<int>(n, default_value));

So, I have the following:

std::vector< std::vector <int> > fog;

and I am initializing it very naively like:

    for(int i=0; i<A_NUMBER; i++)
    {
            std::vector <int> fogRow;
            for(int j=0; j<OTHER_NUMBER; j++)
            {
                 fogRow.push_back( 0 );
            }
            fog.push_back(fogRow);
    }

And it feels very wrong... Is there another way of initializing a vector like this?

This code snippet copies one two-dimensional vector to another. And gives us a clear picture of how to initialize the 2D vector.

void copyVectorToVector(vector<vector<int>> matrix) {

        int rowNumber = matrix.size();
        int columnNumber = matrix[0].size();
        vector<vector<int>> result(rowNumber, vector<int> (columnNumber, 0));

        for(int i=0;i<matrix.size();i++){
            for(int j=0;j<matrix[i].size();j++){
                result[i][j] = matrix[i][j];
                cout<<result[i][j]<<" ";
            }
            cout<<endl;
        }
}

For someone who wants to initialize only the first col values to some different value(say 2) from the rest of the default values(say 1).

int rows = 3, cols = 4;
std::vector<std::vector<int>> matrix(rows, std::vector<int>(cols, 1));  
for (int r = 0 ; r < rows ; r++) matrix[r][0] = 2;
2 1 1 1
2 1 1 1
2 1 1 1

My c++ STL code to initialise 5*3 2-D vector with zero


#include <iostream>
using namespace std;
#include <vector>
int main()
{// if we wnt to initialise a 2 D vector with 0;

    vector<vector<int>> v1(5, vector<int>(3,0));

    for(int i=0;i<v1.size();i++) 
{
        for(int j=0;j<v1[i].size();j++)

           cout<<v1[i][j]<<" ";

            cout<<endl;
    }
}

multiplication table with 2D vector the first 10 is for ROW, second 10 for COLUMN

std::vector<std::vector<int>> multiplication (10,std::vector<int>(10));
for(int i{0}; i<multiplication.size(); ++i){
    for(int j{0}; j<multiplication[i].size(); ++j){
        multiplication[i][j] = j * i;
    }
}

for(int i{1}; i < multiplication.size(); ++i){
    for(int j{1}; j < multiplication[i].size(); ++j){
        std::cout<<multiplication[i][j]<<"\t";
    }
    printf("\n");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM