我的代码有问题:它总是崩溃,但是当我评论时:dataResults [i] .clear();
知道原因吗?
std::vector<std::string> r_OCRtoRetrieve;
std::vector<std::string> DBentries;
//stuff..
int distance = 9999; //TODO change here
int minDistance = 9999;
for(int i=0; i< r_OCRtoRetrieve.size(); i++)
for(int j=0; j< DBentries.size(); j++)
{
distance = calcDistance( (const char *)r_OCRtoRetrieve[i].c_str(),(const char *) DBentries[j].c_str());
if (distance == minDistance)
dataResults[i].push_back(DBentries[j]);
else
if(distance < minDistance)
{
minDistance = distance;
dataResults[i].clear();
dataResults[i].push_back(DBentries[j]);
}
}
编辑:
发现错误..我必须对其进行初始化..这是代码:
for(int i=0; i< r_OCRtoRetrieve.size(); i++)
{
dataResults.push_back(std::vector<std::string>());
for(int j=0; j< DBentries.size(); j++)
{
distance = calcDistance( (const char *)r_OCRtoRetrieve[i].c_str(),(const char *) DBentries[j].c_str());
if (distance == minDistance)
dataResults[i].push_back(DBentries[j]);
else
if(distance < minDistance)
{
minDistance = distance;
if(dataResults[i].size() > 0)
dataResults[i].clear();
dataResults[i].push_back(DBentries[j]);
}
}
}
最佳答案
vector dataResults
的大小可能小于i
的某个值