最近调试一个程序,在使用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::: 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.****.net/ldkcumt/article/details/51396980
https://blog.****.net/oNever_say_love/article/details/50763238