仅在清除后才在向量的向量上崩溃

发布于 2024-11-05 04:21:43 字数 1435 浏览 0 评论 0 原文

我对这段代码有一个问题:它总是崩溃,但是当我评论时: 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]);
    }
    }

  } 

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]);
            }

    }

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]);
    }
    }

  } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我三岁 2024-11-12 04:21:43

向量 dataResults 的大小可能小于 i 的某个值

probably, the size of your vector dataResults is smaller than a certain value of i

不念旧人 2024-11-12 04:21:43

如果您遇到 index out of range 异常,可能是因为您的索引 i 超出了向量 dataResults 的范围(

如果不是的话)很明显,如果 i 大于 dataResults.size(),那么 dataResults[i].clear(); 将抛出异常。

编辑:

考虑用 STL 迭代器替换基于索引的循环,并用 C++ 样式转换替换 c 样式转换。您的 if 语句也可以重新审视...

编辑 2:

猜测可能的问题,因为您没有告诉我们异常是什么,但您有一个 vector> 对吗?如果是这样,您需要在调用方法或构造循环之前检查正在索引到有效位置的内容,以免索引超出范围。

像这样的事情:

if (dataResults.size() > i)
{
   // now we know dataResults[i] will be valid
   dataResults[i].clear();
   // etc
}

老实说,我可能会做更多这样的事情:

    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
        }
}

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
        }
}
把时间冻结 2024-11-12 04:21:43

当您开始时,DataResults 中有什么?任何事物?如果不是,那么第一次通过时,当距离小于 9999 时,代码将尝试清除第一个、第零个索引处不存在的位置并抱怨。

如果您进行调试构建,您应该会收到一条有意义的消息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文