C ++ /二维向量的大小 [英] C++/Size of a 2d vector

查看:196
本文介绍了C ++ /二维向量的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

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

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

注意:要获得 v2d 整体大小,请总结每个单独向量的大小,因为每个向量都有自己的大小。

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

这篇关于C ++ /二维向量的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆