循环内的向量大小

时间:2019-07-08 13:31:04

标签: c++ c++11

嗨,我在使用std :: vector的大小时遇到​​了麻烦,我的代码是这样的:


std::vector<double>
MouseRecords::distanceDuringClickLeft(const std::vector<MouseEvent> &events) {
  std::vector<double> results;
  double movementSum = 0.0;

  for (int i = 0; i < events.size(); ++i) {
    if (events[i].type == EV_MOUSE_LEFT_DOWN) {
      movementSum = 0.0;
      std::vector<MouseEvent> movEvents;

      while ((i < events.size() - 1) &&
      !(events[i].type == EV_MOUSE_LEFT_UP || events[i].type == EV_MOUSE_RIGHT_UP || events[i].type == EV_MOUSE_OTHER_UP)) {
        ++i;
        if (events[i].type == EV_MOUSE_MOVED) {
          movEvents.push_back((events[i]));
        }
      }
      int k = movEvents.size() - 1;
      for (int j = 0; j < k; ++j) {
        movementSum += std::sqrt(std::pow(movEvents[j + 1].axisX - movEvents[j].axisX, 2.0)
                                     + std::pow(movEvents[j + 1].axisY - movEvents[j].axisY, 2.0));
      }

      if (movementSum > 0) {
        results.push_back(movementSum);
      }
      movementSum = 0.0;

    }
  }
  return results;
}

问题出在第二个循环中的movEvents.size()上,它像这样工作,但是像这样使用它:

      for (int j = 0; j < movEvents.size(); ++j) 

完全将其破坏,因为movEvents.size()给出的值不正确(给出一个绝对不是向量大小的巨大数字) 我究竟做错了什么?我宁愿使用movEvents.size()

对其进行设置

0 个答案:

没有答案