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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27340220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:47:02  来源:igfitidea点击:

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

c++vector

提问by jason

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.

对于列数,我想不出什么。你能告诉我我的行数是否正确,你能告诉我如何获得列数吗?谢谢。

采纳答案by Kilppari

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;
}

回答by Manjunath Jakaraddi

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.

这里 v 是一个二维向量,在列大小方面具有不同的大小。使用 v.size() 因为它给出总行数,而 v[i].size() 给出第 i 行的总列数。以下代码可用于迭代不同的二维向量。

回答by Nikhil Arora

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()

要查找 2D vector 中的行数,您可以简单地使用 vector_name.size()。这将返回向量的大小。要查找第 I 行中的列数,请使用 vector_name[i].size()

回答by Shivam Dhanauria

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++.

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