C+ /二维向量的大小

时间:2021-02-13 21:30:02

How do I find the size of a 2 dimensional vector? So far I have the following code which doesn't compile.

如何求二维向量的大小?到目前为止,我有以下未编译的代码。

#include <iostream>
#include <vector>

using namespace std;

int main()
{

    vector < vector <int> > v2d;

    for (int x = 0; x < 3; x++)
    {
        for (int y = 0; y < 5; y++)
        {
            v2d.push_back(vector <int> ());
            v2d[x].push_back(y);
        }
    }

    cout<<v2d[0].size()<<endl;
    cout<<v2d[0][0].size()<<endl;

    return 0;
}

4 个解决方案

#1


27  

To get the size of v2d, simply use v2d.size(). For size of each vector inside v2d, use v2d[k].size().

要获得v2d的大小,只需使用v2d.size()。对于v2d中每个向量的大小,使用v2d[k].size()。

Note: for getting the whole size of v2d, sum up the size of each individual vector, as each vector has its own size.

注意:为了得到v2d的整个大小,要总结每个向量的大小,因为每个向量都有它自己的大小。

#2


10  

You had some errors in your code, which I've fixed and commented on below.

您的代码中有一些错误,我在下面对它们进行了修正和注释。

vector < vector <int> > v2d;

for (int x = 0; x < 3; x++)
{
    // Move push_back() into the outer loop so it's called once per
    // iteration of the x-loop
    v2d.push_back(vector <int> ());
    for (int y = 0; y < 5; y++)
    {
        v2d[x].push_back(y);
    }
}

cout<<v2d.size()<<endl; // Remove the [0]
cout<<v2d[0].size()<<endl; // Remove one [0]

v2d.size() returns the number of vectors in the 2D vector. v2d[x].size() returns the number of vectors in "row" x. If you know the vector is rectangular (all "rows" are of the same size), you can get the total size with v2d.size() * v2d[0].size(). Otherwise you need to loop through the "rows":

size()返回二维向量中的向量个数。v2d[x].size()返回“行”x中向量的个数,如果知道向量是矩形的(所有的“行”都是相同的大小),就可以得到v2d.size() * v2d[0].size()的总大小。否则,您需要遍历“行”:

int size = 0;
for (int i = 0; i < v2d.size(); i++)
    size += v2d[i].size();

As a change, you can also use iterators:

作为改变,您还可以使用迭代器:

int size = 0;
for (vector<vector<int> >::const_iterator it = v2d.begin(); it != v2d.end(); ++it)
    size += it->size();

#3


8  

The vector<vector<int>> does not have a whole size, because each vector within it has an independent size. You need to sum the size of all contained vectors.

向量 >没有一个完整的大小,因为它里面的每个向量都有一个独立的大小。你需要对所有包含向量的大小进行求和。

int size = 0;
for(int i = 0; i < v2d.size(); i++)
    size += v2d[i].size();

#4


-1  

int size_row = v2d.size();
int size_col = v2d[0].size();

#1


27  

To get the size of v2d, simply use v2d.size(). For size of each vector inside v2d, use v2d[k].size().

要获得v2d的大小,只需使用v2d.size()。对于v2d中每个向量的大小,使用v2d[k].size()。

Note: for getting the whole size of v2d, sum up the size of each individual vector, as each vector has its own size.

注意:为了得到v2d的整个大小,要总结每个向量的大小,因为每个向量都有它自己的大小。

#2


10  

You had some errors in your code, which I've fixed and commented on below.

您的代码中有一些错误,我在下面对它们进行了修正和注释。

vector < vector <int> > v2d;

for (int x = 0; x < 3; x++)
{
    // Move push_back() into the outer loop so it's called once per
    // iteration of the x-loop
    v2d.push_back(vector <int> ());
    for (int y = 0; y < 5; y++)
    {
        v2d[x].push_back(y);
    }
}

cout<<v2d.size()<<endl; // Remove the [0]
cout<<v2d[0].size()<<endl; // Remove one [0]

v2d.size() returns the number of vectors in the 2D vector. v2d[x].size() returns the number of vectors in "row" x. If you know the vector is rectangular (all "rows" are of the same size), you can get the total size with v2d.size() * v2d[0].size(). Otherwise you need to loop through the "rows":

size()返回二维向量中的向量个数。v2d[x].size()返回“行”x中向量的个数,如果知道向量是矩形的(所有的“行”都是相同的大小),就可以得到v2d.size() * v2d[0].size()的总大小。否则,您需要遍历“行”:

int size = 0;
for (int i = 0; i < v2d.size(); i++)
    size += v2d[i].size();

As a change, you can also use iterators:

作为改变,您还可以使用迭代器:

int size = 0;
for (vector<vector<int> >::const_iterator it = v2d.begin(); it != v2d.end(); ++it)
    size += it->size();

#3


8  

The vector<vector<int>> does not have a whole size, because each vector within it has an independent size. You need to sum the size of all contained vectors.

向量 >没有一个完整的大小,因为它里面的每个向量都有一个独立的大小。你需要对所有包含向量的大小进行求和。

int size = 0;
for(int i = 0; i < v2d.size(); i++)
    size += v2d[i].size();

#4


-1  

int size_row = v2d.size();
int size_col = v2d[0].size();