這將會很長,所以我提前道歉。但是我想確認上下文是可以理解的,因爲我已經閱讀了許多關於這個主題的文章。我沒有發現解決數字基於未知數字範圍的問題。C++ int發生次數未知範圍
我想確定一個集合的每個整數的總出現次數。我遇到的問題是,測試集是固定的,例如10個數字的數組,但每個數字的範圍是未知的。提出的問題是,當試圖使用數組來計算總數時,數組範圍在運行時不能變化。我嘗試使用vector<int> ArrayName
重試此嘗試,但我可以在運行時重新調整總計數組大小,但在計算中使用vector<int>
值時會遇到錯誤。
我將介紹的代碼是使用OpenCV
進行人臉檢測。我研究並利用各種樣本的代碼創建了一個基本的檢測程序,然後研究並移植了一些Java代碼,以便在面部移動並更新到新位置時處理跟蹤。所有這些代碼正在工作。
我想在哪裏使用請求的信息,是我想存儲最後一個數組,例如10
,檢測到一個臉部的主體,並找到一個隨時間檢測到最多的主體並將其視爲主體。假設我們正在檢測10個,並且最後十個幀被檢測到如下(-1是未知的面):-1, -1, 0, 0, 0, 3, 0, -1, 0, 2
。在發送之後,0
發生得最爲頻繁,因此主題爲0
。範圍未知的原因是因爲主題ID取決於受過訓練的主題數量,並且一直在變化。
三個錯誤(以及//錯誤表示):
invalid use of member (did you forget the '&' ?),
no match for call to '(std::vector<int>) (int)',
no match for call to '(std::vector<int>) (int)&'
下面是代碼:
struct FaceStruct {
private:
static const int NewLife = 120; //Divide by FPS for Length in Time
static const int TotalTrackedSubjects = 10;
int Life;
vector<int> Subjects;
public:
void Init(Rect, int);
void addSubject(int);
int Subject();
Rect Location;
bool Used;
void Weaken();
void Renew();
bool Dead();
};
void FaceStruct::Init(Rect location, int subject = -1) {
Location = location;
Subjects.resize(TotalTrackedSubjects - 1);
for(int i = 0; TotalTrackedSubjects - 1; i++) {
Subjects(i) = -1; //ERROR
}
Renew();
}
void FaceStruct::addSubject(int subject) {
for(int i = 0; TotalTrackedSubjects - 2; i++) {
Subjects(i) = Subjects(i + 1); //ERROR
}
Subjects(TotalTrackedSubjects - 1) = subject; //ERROR
}
int FaceStruct::Subject() {
int count_range = -1;
for(int i = 0; TotalTrackedSubjects - 1; i++) {
if(Subjects(i) > count_range) count_range = Subjects(i); //ERROR
}
if(count_range < 0) { //Subject Unknown
return -1;
} else if(count_range == 0) { //Subject is 0, Handle 0's
int totals = 0;
for(int i = 0; TotalTrackedSubjects - 1; i++) {
if(Subjects(i) == 0) totals++; //ERROR
}
return totals;
} else { //Use count_range
vector<int> totals;
int unknowns = 0;
totals.resize(count_range);
for(int i = 0; TotalTrackedSubjects - 1; i++) {
if(Subjects(i) < 0) { //ERROR
unknowns++;
} else {
totals(Subjects(i)) = totals(Subjects(i)) + 1; //ERROR
}
}
int largest = -1;
for(int i = 0; totals.size() - 1; i++) {
if(totals(i) > largest) largest = totals(i); //ERROR
}
return largest;
}
}
void FaceStruct::Weaken() {
Life--;
}
void FaceStruct::Renew() {
Life = NewLife;
}
bool FaceStruct::Dead() {
if(Life < 1) {
return true;
} else {
return false;
}
}
我想你在繼續閱讀之前閱讀[一些C++書籍](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。您的for循環何時停止? –
我以前從未遇到過問題。 TotalTrackedSubjects在Struct的頂部被定義爲一個const int 10,那麼它會以10 - 1結束...不是? –
如果這就是你認爲的那樣,這並不是從0到9。 – Matt