Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement 2D vector array?

Tags:

c++

vector

I'm using the vector class in the STL library for the first time. How should I add to a specific row of the vector array?

struct x{
     vector <vector <int> > v;
     int row;
 };

vector< int* > my ints;
int add;

if i wanted to add to first row of v with the first pointer of integers, could I do

myints[0]->v[myints[0]->row].push_back(add);

Is this method fine to create a 2 D vector of vector ints where each row could potentially be of different length (i.e. have a different number of columns)?

like image 234
John Smith Avatar asked Mar 14 '12 01:03

John Smith


People also ask

How do you make a 2D array vector?

2D vectors are often treated as a matrix with “rows” and “columns” inside it. Under the hood they are actually elements of the 2D vector. We first declare an integer variable named “row” and then an array named “column” which is going to hold the value of the size of each row.

How do you add a vector to a 2D vector?

Insertion in Vector of Vectors Elements can be inserted into a vector using the push_back() function of C++ STL. Below example demonstrates the insertion operation in a vector of vectors. The code creates a 2D vector by using the push_back() function and then displays the matrix.

How does 2D vector work?

2-Dimensional Vector, also known as a vector of vectors is a vector with an adjustable number of rows where each of the rows is a vector. Here, each vector index stores a vector that can be traversed as well as accessed with the help of iterators.


9 Answers

I'm not exactly sure what the problem is, as your example code has several errors and doesn't really make it clear what you're trying to do. But here's how you add to a specific row of a 2D vector:

// declare 2D vector
vector< vector<int> > myVector;
// make new row (arbitrary example)
vector<int> myRow(1,5);
myVector.push_back(myRow);
// add element to row
myVector[0].push_back(1);

Does this answer your question? If not, could you try to be more specific as to what you are having trouble with?

like image 132
Moritz Avatar answered Oct 05 '22 02:10

Moritz


If you know the (maximum) number of rows and columns beforehand, you can use resize() to initialize a vector of vectors and then modify (and access) elements with operator[]. Example:

int no_of_cols = 5;
int no_of_rows = 10;
int initial_value = 0;

std::vector<std::vector<int>> matrix;
matrix.resize(no_of_rows, std::vector<int>(no_of_cols, initial_value));

// Read from matrix.
int value = matrix[1][2];

// Save to matrix.
matrix[3][1] = 5;

Another possibility is to use just one vector and split the id in several variables, access like vector[(row * columns) + column].

like image 20
Roi Danton Avatar answered Oct 05 '22 01:10

Roi Danton


Just use the following methods to create a 2-D vector.

int rows, columns;        

// . . .

vector < vector < int > > Matrix(rows, vector< int >(columns,0));

OR

vector < vector < int > > Matrix;

Matrix.assign(rows, vector < int >(columns, 0));

//Do your stuff here...

This will create a Matrix of size rows * columns and initializes it with zeros because we are passing a zero(0) as a second argument in the constructor i.e vector < int > (columns, 0).

like image 27
Aditya Goel Avatar answered Oct 05 '22 02:10

Aditya Goel


//initialize the 2D vector first

vector<vector<int>> matrix;

//initialize the 1D vector you would like to insert into matrix

vector<int> row;

//initializing row with values

row.push_back(val1);

row.push_back(val2);

//now inserting values into matrix

matrix.push_back(row);

//output- [[val1,val2]]

like image 41
Madhurya Gandi Avatar answered Oct 05 '22 01:10

Madhurya Gandi


We can easily use vector as 2d array. We use resize() method for this purpose. The code below can be helpful to understand this issue.

Code Snippet :

#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int row, col;
    cin>>row>>col;

    vector <vector<int>> v;
    v.resize(col,vector<int>(row));

    //v = {{1,2,3}, {4,5,6}, {7,8,9}}; 

    /** input from use **/
    for(int i=0; i<row; i++)
    {
       for(int j=0; j<col; j++)
       {
          cin>>v[i][j];
       }
    }

    for(int i=0;i<row; i++)
    {
       for(int j=0;j<col;j++)
       {
          cout<<v[i][j]<<" ";
       }
    }
    return 0;
}
like image 43
rashedcs Avatar answered Oct 05 '22 01:10

rashedcs


Another way to define a 2-d vector is to declare a vector of pair's.

 vector < pair<int,int> > v;

**To insert values**
 cin >> x >>y;
 v.push_back(make_pair(x,y));

**Retrieve Values**
 i=0 to size(v)
 x=v[i].first;
 y=v[i].second;

For 3-d vectors take a look at tuple and make_tuple.

like image 21
Varun Garg Avatar answered Oct 05 '22 00:10

Varun Garg


I use this piece of code . works fine for me .copy it and run on your computer. you'll understand by yourself .

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector <vector <int> > matrix;
    size_t row=3 , col=3 ;
    for(int i=0,cnt=1 ; i<row ; i++)
    {
        for(int j=0 ; j<col ; j++)
        {
            vector <int> colVector ;
            matrix.push_back(colVector) ;
            matrix.at(i).push_back(cnt++) ;
        }
    }
    matrix.at(1).at(1) = 0;     //matrix.at(columns).at(rows) = intValue 
    //printing all elements
    for(int i=0,cnt=1 ; i<row ; i++)
    {
        for(int j=0 ; j<col ; j++)
        {
            cout<<matrix[i][j] <<" " ;
        }
        cout<<endl ;
    }
}
like image 22
Ridowan Ahmed Avatar answered Oct 05 '22 02:10

Ridowan Ahmed


vector<vector> matrix(row, vector(col, 0));

This will initialize a 2D vector of rows=row and columns = col with all initial values as 0. No need to initialize and use resize.

Since the vector is initialized with size, you can use "[]" operator as in array to modify the vector.

matrix[x][y] = 2;

like image 26
Satakshi Pandey Avatar answered Oct 05 '22 00:10

Satakshi Pandey


vector<int> adj[n]; // where n is number of rows in 2d vector.

like image 28
Abhishek Kumar Avatar answered Oct 05 '22 02:10

Abhishek Kumar