• 主页
• 问答

# C++ vector 转化成数组，push_back函数怎样转化，加断点监测变量值发现不一样

RoseZhouZhou 北京工业大学 学生  2016-12-22 03:35:30
C++ vector 转化成数组，push_back函数怎样转化，加断点监测变量值发现不一样。

``````
double SJN_MultiCueBGS::CalculateHausdorffDist(IplImage* input_image, IplImage* model_image){

//Step1: Generating imag vectors
//For reduce errors, points at the image boundary are excluded
vector<point> vInput, vModel;
point temp;

//input image --> input vector
for (int i = 0; i < input_image->height; i++){
for (int j = 0; j < input_image->width; j++){

if ((uchar)input_image->imageData[i*input_image->widthStep + j] == 0) continue;

temp.m_nX = j; temp.m_nY = i;
vInput.push_back(temp);
}
}
//model image --> model vector
for (int i = 0; i < model_image->height; i++){
for (int j = 0; j < model_image->width; j++){
if ((uchar)model_image->imageData[i*model_image->widthStep + j] == 0) continue;

temp.m_nX = j; temp.m_nY = i;
vModel.push_back(temp);
}
}

if (vInput.empty() && !vModel.empty()) return (double)vModel.size();
else if (!vInput.empty() && vModel.empty()) return (double)vInput.size();
else if (vInput.empty() && vModel.empty()) return 0.0;

//Step2: Calculating forward distance h(Model,Image)
double dDist, temp1, temp2, dMinDist;
vector<double> vTempDist;

for (auto iter_m = vModel.begin(); iter_m < vModel.end(); iter_m++){

dMinDist = 9999999;
for (auto iter_i = vInput.begin(); iter_i < vInput.end(); iter_i++){
temp1 = (*iter_m).m_nX - (*iter_i).m_nX;
temp2 = (*iter_m).m_nY - (*iter_i).m_nY;
dDist = temp1*temp1 + temp2*temp2;

if (dDist < dMinDist) dMinDist = dDist;
}
vTempDist.push_back(dMinDist);
}
sort(vTempDist.begin(), vTempDist.end()); //in ascending order

double dQuantileVal = 0.9, dForwardDistance;
int iDistIndex = (int)(dQuantileVal*vTempDist.size()); if (iDistIndex == vTempDist.size()) iDistIndex -= 1;

dForwardDistance = sqrt(vTempDist[iDistIndex]);
return dForwardDistance;

}
``````

``````
int m_size=0;
void sort_vTempDist(double *vTempDist)
{
int i, j;
double t;
for (i = 0;i < m_size - 1;i++)
{
for (j = 0; j < m_size - 1 - i; j++)
{
if (vTempDist[j]>vTempDist[j + 1])
{
t = vTempDist[j];
vTempDist[j] = vTempDist[j + 1];
vTempDist[j + 1] = t;
}
}
}
}
double CalculateHausdorffDist(IplImage* input_image, IplImage* model_image) {

//vector<point> vInput, vModel;一维向量-用来代替一维数组
point *vInput;
point *vModel;
point temp;
vInput = (point*)malloc(sizeof(struct point)*150);
vModel= (point*)malloc(sizeof(struct point)*150);
int m = 0,n=0;
for (int i = 0; i < input_image->height; i++) {
for (int j = 0; j < input_image->width; j++) {

if ((uchar)input_image->imageData[i*input_image->widthStep + j] == 0) continue;

temp.m_nX = j; temp.m_nY = i;
//vInput.push_back(temp);在尾部插入一个元素
vInput[j]=temp;
n= j;
}
}

for (int i = 0; i < model_image->height; i++) {
for (int j = 0; j < model_image->width; j++) {
if ((uchar)model_image->imageData[i*model_image->widthStep + j] == 0) continue;

temp.m_nX = j; temp.m_nY = i;
//vModel.push_back(temp);在尾部插入一个元素，下面是我转换的
vModel[j]= temp;
m = j;
}
}
//.empty判断容器是否为空  .size返回容器实际数据个数
if (vInput==NULL && vModel!=NULL) return (double)(m+1);
else if (vInput != NULL &&  vModel == NULL) return (double)(n+1);
else if (vInput == NULL && vModel == NULL) return 0.0;

//Step2: Calculating forward distance h(Model,Image)
double dDist, temp1, temp2, dMinDist;
//vector<double> vTempDist;
double *vTempDist;
vTempDist = (double *)malloc(sizeof(double)*150);
//遍历结构体数组与遍历普通数组方法一样,m+1为vModel中的个数，n+1为vInput中的个数
int k=0;
for (auto iter_m = vModel; iter_m <(vModel+m+1); iter_m ++) {

dMinDist = 9999999;
for (auto iter_i = vInput; iter_i < (vInput+n+1); iter_i++) {
temp1 = (*iter_m).m_nX - (*iter_i).m_nX;
temp2 = (*iter_m).m_nY - (*iter_i).m_nY;
dDist = temp1*temp1 + temp2*temp2;
if (dDist < dMinDist) dMinDist = dDist;
}
vTempDist[k]=dMinDist;
k++;
}
m_size = k;//用来记录vTempDist数组中元素的个数
//sort为vector自带函数，将元素按升序排列
//sort(vTempDist.begin(), vTempDist.end()); //in ascending order
sort_vTempDist(vTempDist);
double dQuantileVal = 0.9, dForwardDistance;
int iDistIndex = (int)(dQuantileVal*k); if (iDistIndex == m_size) iDistIndex -= 1;
dForwardDistance = sqrt(vTempDist[iDistIndex]);
free(vInput);
free(vModel);
free(vTempDist);
return dForwardDistance;

}
``````
...全文
129 点赞 收藏 2

2 条回复

RoseZhouZhou 2016-12-22

malloc、realloc、free

C语言

6.2w+

C语言相关问题讨论