C++/二维向量的大小

发布于 2024-10-09 01:53:20 字数 492 浏览 0 评论 0 原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断爱 2024-10-16 01:53:20

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

注意:要获取 v2d整体大小,请将每个向量的大小相加,因为每个向量都有自己的大小。

To get the size of v2d, simply use v2d.size().
For size of each vector inside v2d, use 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.

辞慾 2024-10-16 01:53:20

您的代码中有一些错误,我已在下面修复并评论了这些错误。

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() 返回 2D 向量中向量的数量。 v2d[x].size() 返回“行”x 中向量的数量。如果您知道向量是矩形(所有“行”的大小相同),则可以使用 v2d.size() * v2d[0].size() 获取总大小。否则,您需要循环遍历“行”:

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

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

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

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":

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();
晨曦慕雪 2024-10-16 01:53:20

vector> 没有完整的大小,因为其中的每个向量都有独立的大小。您需要对所有包含的向量的大小求和。

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

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();
忆梦 2024-10-16 01:53:20
int size_row = v2d.size();
int size_col = v2d[0].size();
int size_row = v2d.size();
int size_col = v2d[0].size();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文