如何初始化向量的向量? [英] How to initialize a vector of vectors?

查看:145
本文介绍了如何初始化向量的向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个NxN矩阵

If I have a NxN matrix

vector< vector<int> > A;

如何初始化?

尝试没有成功:

 A = new vector(dimension);
 A = new vector(dimension,vector<int>(dimension));


推荐答案

您使用 code>执行动态分配。它返回一个指向动态分配的对象的指针。

You use new to perform dynamic allocation. It returns a pointer that points to the dynamically allocated object.

你没有理由使用 new code> A 是一个自动变量。您可以使用其构造函数简单地初始化 A

You have no reason to use new, since A is an automatic variable. You can simply initialise A using its constructor:

vector<vector<int> > A(dimension, vector<int>(dimension));

这篇关于如何初始化向量的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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