Column size and row size of a 2D vector in C++
Asked Answered
U

8

38

I have a vector like this :

vector< vector<int> > myVector;

All row and column numbers are same in this vector.

I want to find row count and column count of this vector.

For row count I come up with :

myVector[0].size();

For column count, I can't come up with anything. Can you tell me if my row count is correct and can you tell me how I can get column count? Thanks.

Ursal answered 7/12, 2014 at 6:19 Comment(1)
you mean you have come up with a way to find column count , myVector[0].size(), will return number of columns in first row and not the number of rows.Mottle
B
46

You have a vector of integer vectors myVector[0].size() returns you the amount of elements in the first int vector in the 2d vector.

The structure of such vector looks like this:

myVector[
  Vector[0, 4, 2, 5],
  Vector[1, 4, 2]
];

When you call for myVector[1].size() it would return 3 and [0] would return 4.

For the amount of rows (int vectors) in the 2d vector, you can just use myVector.size()

You can run this to see it in actions

#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>>MyVector;
    std::vector<int>temp;

    temp.push_back(1);
    temp.push_back(2);
    temp.push_back(3);
    MyVector.push_back(temp);

    std::cout << "Rows in the 2d vector: " << MyVector.size() <<
    std::endl << "Collumns in the 1st row: " << MyVector[0].size() <<
    std::endl;

    system("pause");
    return 0;
}

This is the output:

Rows in the 2d vector: 1
Collumns in the 1st row: 3
Brainwork answered 7/12, 2014 at 7:38 Comment(0)
P
7
for(int i=0;i<v.size();i++){
    for(int j=0;j<v[i].size();j++){
        cout<<v[i][j]<<" ";
    }
    cout<<endl;
}

Here v is a two dimensional vector of varying size in terms of column size. Use v.size() as it gives the total number of rows and v[i].size() gives you the total number of colums in ith row. The following code can be used to iterate through varying two dimensional vector.

Priscella answered 1/5, 2017 at 7:12 Comment(0)
N
5

In C++:

vector<vector < int > > matrix;

cout << "Row's Length: " << matrix.size();
cout<< "Column's Length: "<< matrix[0].size();

In JAVA:

int[][] matrix;

System.out.println("Row's Length: " + matrix.length);
System.out.println("Column's Length: " + matrix[0].length);
Nisbet answered 5/4, 2021 at 15:26 Comment(3)
You may want to re-write/edit Java print statements.Cannibal
Bro Thanks for your Advice😊Nisbet
You can format your code anytime, done it for you this time & upvoted. :)Cannibal
G
2

To find the number of rows in a 2D vector , you can simply use vector_name.size(). this will return the size of vector. to find number of columns in Ith row use vector_name[i].size()

Grati answered 16/9, 2018 at 11:44 Comment(2)
Hello, your answer is far more detailled that the already accepted answer. Could you edit your post to add new information ?Cooksey
I gave this answer just to summarize the answer in very simple and precise words . @CookseyGrati
H
2
int main(){
    vector<vector<int>>test{{1,2},
                            {5,6},
                            {9,10}};// 3 rows and 2 columns
    cout<<"Columns: "<<test[0].size();// 2
    cout<<"Rows: "<<test.size();// 3
    return 0;
}

The above is only true when every row has the same number of elements. If different rows have different elements, you can find the number of elements in each row using a for loop.

Hopeless answered 3/10, 2020 at 3:19 Comment(0)
M
1

vector_name.size() gives you the numbers of column in a 2D vector and vector_name[0].size() gives you the numbers of rows in a 2D vector in C++.

Microsurgery answered 3/2, 2020 at 13:33 Comment(1)
I think you meant "vector_name.size() gives the number of rows in a 2D vector and vector_name[0].size() gives you the number of columns in a 2D vector in C++"Latakia
E
0

For 2D vector-like, vector< vector<int> > myVector at first you may notice a pattern that is, the vector of vector, the first vector is the base vector, the second one is on top of that vector, so in case of array-like, arr[5][6] 5 is no of the row, 6 is the number of columns. so similar to vector< vector<int> > myVector you may think myVector.size() give you the row number, and on the other side myVector[0].size() or myVector[n].size() any valid range of row "n" give you the column number of that particular row. If columns and rows no are the same, then just one check can solve your problem.

Emendation answered 29/8, 2020 at 7:38 Comment(0)
C
0

I think your question is that you want to input the size of 2D vector and also the initialization right!!!

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

int main(){

    vector<vector<int>> adjm(n+1, vector<int> (n+1, 0));



    return 0;
}

This would solve your question This is how you get input the adjacency matrix size of rows and columns

Charissacharisse answered 3/9, 2022 at 18:35 Comment(1)
the question: "I want to find row count and column count of this vector.".Thundery

© 2022 - 2024 — McMap. All rights reserved.