如何在循环

时间:2016-05-04 08:54:44

标签: c++ c++11

我想用一些信息填充一个名为firstApp的向量(id,position(x,y),frameNumber)。所以我创建一个条件以避免使用现有信息填充向量。

std::vector<FirstAppearance>::iterator p;
for (int idxCurrent = 0; idxCurrent < nOfPedcurrFrame ;  idxCurrent++) 
{


            if(nOfPedprevFrame == 0)
            {
                CurrentId=frames[frameNumber].GetPedest()[idxCurrent].GetID();  

                FA=FirstAppear(frames,CurrentId,idxCurrent);

                 FirstPosition=frames[FA].GetPedest()[idxCurrent].GetPosition();

                f.SetFrameNumber(FA);
                f.SetFirstPosition(FirstPosition);
                f.SetFId(CurrentId);


                if(firstApp.size() !=0 )
                {


                    p=std::find(firstApp.begin(),firstApp.end(),f);
                    std::cout << *p<< endl;


                    if(p == firstApp.end())
                    {
                        firstApp.push_back(f);

                    }
                }
                else
                {

                    firstApp.push_back(f);

                }


                f.~FirstAppearance();
             }
}

FirstAppearnce.cpp:

 #include "stdafx.h"
 #include "FirstAppearance.h"
 #include <iostream>

FirstAppearance::FirstAppearance(){}
int FirstAppearance::GetFId(void)const {return fId;}
objectCoordinates FirstAppearance::GetFirstPosition(void)const {return firstPosition;}
void FirstAppearance::SetFirstPosition(objectCoordinates pos){firstPosition =pos;}
void FirstAppearance::SetFId(int id){fId =id;}
int FirstAppearance::GetFrameNumber(void)const {return fNumber;}
void FirstAppearance::SetFrameNumber(int fnumb){ fNumber = fnumb;}
bool FirstAppearance::operator==(const FirstAppearance& a){

    if((firstPosition.x == a.firstPosition.x) && (firstPosition.y == a.firstPosition.y) &&
    (fId==a.fId) && (fNumber==a.fNumber)){
        return true;        
    }
}
FirstAppearance::~FirstAppearance(){}

我使用std :: find来搜索我的向量中是否存在f以填充它。但是当我编译我的程序时,我得到的p具有这样的值,即我程序生命周期中f的第一个值。可以任何一个告诉我为什么std :: find没有用新值更新f的值? * p的输出是:

1 263 138 0
1 263 138 0
1 263 138 0
1 263 138 0
1 263 138 0

2 个答案:

答案 0 :(得分:0)

您没有正确检查结果。 你应该有类似的东西:

p=std::find(firstApp.begin(),firstApp.end(),f);
if (p == firstApp.end()) {
  std::cout << f << " not found." << endl;
  firstApp.push_back(f);
} else {
  std::cout << *p << endl;    
}

答案 1 :(得分:0)

bool FirstAppearance::operator==(const FirstAppearance& a)
{

if((firstPosition.x == a.firstPosition.x) && (firstPosition.y == a.firstPosition.y) &&
(fId==a.fId) && (fNumber==a.fNumber)){
    return true;        
}
else
{
    return false;
}
}