crash on a vector of vector only after a clear
I've a problem with this code: it crashes always but when I comment: dataResults[i].clear();
Any idea about the reason?
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]开发者_如何学Python);
}
}
Edit:
Found the error.. I had to initialize it.. here it is the code:
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]);
}
}
}
probably, the size of your vector dataResults
is smaller than a certain value of i
If you're getting an index out of range exception it's probably because your index i
is out of range of the vector dataResults
If that wasn't obvious enough basically if i
is greater than dataResults.size()
then dataResults[i].clear();
will throw an exception.
Edit:
Consider replacing your index based loops with STL iterators and replacing your c-style casting with C++ style casting. Your if statement could be revisited as well ...
Edit 2:
Was a guessing at a likely problem as you didn't tell us what the exception was but you have a vector<vector<string>>
right? If so you need to check what you are indexing into a valid position before calling methods or construct your loop so that it will not index out of bounds.
Something like so:
if (dataResults.size() > i)
{
// now we know dataResults[i] will be valid
dataResults[i].clear();
// etc
}
Honestly I'd probably do something more along the lines of this:
typedef std::vector<std::string> StrArray;
for(StrArray::const_iterator ret(r_OCRtoRetrieve.begin()); ret != r_OCRtoRetrieve.end(); ++ret)
{
// ret will be an const iterator to each string element in r_OCRtoRetrieve
for(StrArray::const_iterator entry(DBentries.begin()); entry != DBentries.end(); ++entry)
{
// entry will be an const iterator to each string element in DBentries
distance = calcDistance(ret->c_str(), entry->c_str());
// init new StrArray in dataResults as needed
// set new min distances as needed
// push back strings to dataResults
// whatever else you want to do
// yata yata
}
}
What is in DataResults when you start? Anything? If not, then then the first time through, when distance is anything less than 9999 then the code will attempt to clear a nonexistent location at the first, zeroth index and complain.
If you do a debug build, you should get a meaningful message.
精彩评论