c++中用vector创建多维数组的初始化方法

最近调试一个程序,在使用vector声明一个二维数组时出现错误。错误的方法如下所示:

std::vector<std::vector<double> > sphereGrid;
int gridLA = angleSpanLA / angelAccuracy;
int gridLO = angleSpanLO / angelAccuracy;
sphereGrid = std::vector<std::vector<double> >( gridLA , gridLO );

会出现如下报错:

/home/zn/VanishingPointDetection/src/VPDetection.cpp: In member function ‘void VPDetection::getSphereGrids(std::vector<std::vector<double> >&)’:
/home/zn/VanishingPointDetection/src/VPDetection.cpp:156:66: error: no matching function for call to ‘std::vector<std::vector<double> >::vector(int&, int&)’
  sphereGrid = std::vector<std::vector<double> >( gridLA , gridLO );

这就是因为二维数组的初始化出现了错误,一般的话要通过下面这种方式初始化

定义空二维vector,再赋值
vector<vector <int> > ivec(m ,vector<int>(n)); //m*n的二维vector,注意两个 "> "之间要有空格!

所以我们要把程序改为

std::vector<std::vector<double> > sphereGrid;
int gridLA = angleSpanLA / angelAccuracy;
int gridLO = angleSpanLO / angelAccuracy;
sphereGrid = std::vector<std::vector<double> >( gridLA , std::vector<double>(gridLO)  );

就可以解决错误,通过这次改错更加认识到了c++之vector的用法。

参考:https://blog.csdn.net/ldkcumt/article/details/51396980

https://blog.csdn.net/oNever_say_love/article/details/50763238

转载于:https://www.cnblogs.com/feifanrensheng/p/8711601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值