简体   繁体   English

C++ 中二维向量的列大小和行大小

[英]Column size and row size of a 2D vector in C++

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.谢谢。

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.您有一个整数向量向量myVector[0].size()返回二维向量中第一个 int 向量中的元素数量。

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.当您调用 myVector[1].size() 时,它将返回 3,而 [0] 将返回 4。

For the amount of rows (int vectors) in the 2d vector, you can just use myVector.size()对于二维向量中的行数(整数向量),您只需使用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
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.这里 v 是一个二维向量,在列大小方面有不同的大小。 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.使用 v.size() 因为它给出总行数,而 v[i].size() 给出第 i 行的总列数。 The following code can be used to iterate through varying two dimensional vector.以下代码可用于迭代不同的二维向量。

In C++:在 C++ 中:

vector<vector < int > > matrix;

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

In JAVA:在 Java 中:

int[][] matrix;

System.out.println("Row's Length: " + matrix.length);
System.out.println("Column's Length: " + matrix[0].length);

To find the number of rows in a 2D vector , you can simply use vector_name.size().要查找 2D vector 中的行数,您可以简单地使用 vector_name.size()。 this will return the size of vector.这将返回向量的大小。 to find number of columns in Ith row use vector_name[i].size()要查找第 I 行中的列数,请使用 vector_name[i].size()

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.如果不同的行有不同的元素,您可以使用 for 循环查找每行中的元素数。

vector_name.size()为您提供二维向量中的列数,而vector_name[0].size()为您提供 C++ 中二维向量中的行数。

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.对于二维向量, vector< vector<int> > myVector首先你可能会注意到一个模式,即向量的向量,第一个向量是基向量,第二个是在该向量之上,以防万一类数组, arr[5][6] 5 是行数,6 是列数。 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.vector< vector<int> > myVector类似,您可能认为myVector.size()为您提供行号,而另一方面myVector[0].size()myVector[n].size()任何有效范围行“n”为您提供该特定行的列号。 If columns and rows no are the same, then just one check can solve your problem.如果列和行编号相同,那么只需一项检查就可以解决您的问题。

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这将解决您的问题这就是您输入行和列的邻接矩阵大小的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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