每次调用方法时增加变量
Increment variable on every call of method
我有这个方法:
void Session::onNewImage(cv::Mat& img, double elapsedTime){
static int count = 0;
add(img, dnnOutput[count++], curLati, curLongi, curAlti, curHeading, curRoll);
}
它被调用了 1400 次。每次 "count" 的值递增。但是当它到达 1401 时,我希望 "count" 变成 0 ,然后在那里再次增加表格。我不希望 "count" 成为全局变量。我怎样才能做到这一点?
P.S。我不能将它硬编码为 1400。每次都可能不同。还有另一种方法决定调用此方法的次数,具体取决于作为该方法输入的图像数量。
应该这样做:
void Session::onNewImage(cv::Mat& img, double elapsedTime){
static int count = 0;
if (count >= getYourCountLimitFromSomewhere())
count = 0;
add(img, dnnOutput[count++], curLati, curLongi, curAlti, curHeading, curRoll);
}
请注意,正如@Aconcagua 在评论中指出的那样,count
与阈值的比较是通过 >
还是 >=
取决于 getYourCountLimitFromSomewhere()
return 值。
我有这个方法:
void Session::onNewImage(cv::Mat& img, double elapsedTime){
static int count = 0;
add(img, dnnOutput[count++], curLati, curLongi, curAlti, curHeading, curRoll);
}
它被调用了 1400 次。每次 "count" 的值递增。但是当它到达 1401 时,我希望 "count" 变成 0 ,然后在那里再次增加表格。我不希望 "count" 成为全局变量。我怎样才能做到这一点?
P.S。我不能将它硬编码为 1400。每次都可能不同。还有另一种方法决定调用此方法的次数,具体取决于作为该方法输入的图像数量。
应该这样做:
void Session::onNewImage(cv::Mat& img, double elapsedTime){
static int count = 0;
if (count >= getYourCountLimitFromSomewhere())
count = 0;
add(img, dnnOutput[count++], curLati, curLongi, curAlti, curHeading, curRoll);
}
请注意,正如@Aconcagua 在评论中指出的那样,count
与阈值的比较是通过 >
还是 >=
取决于 getYourCountLimitFromSomewhere()
return 值。