I'm trying to compare elements of the "coordinateList" vector with elements of "Buttons" vector and if four different if-statements are okay to pass them, increase 1 in "countList" vector. But, the problem is that whenever I input 2 numbers for "coordinateList" which can't pass the if-statement, it gets "vector subscript out of range" on the part;
if ((coordinateList[j][0] >= Buttons[i][0])
&& (coordinateList[j][0] <= Buttons[i][1])
&& (coordinateList[j][1] >= Buttons[i][2])
&& (coordinateList[j][1] <= Buttons[i][3]))
I don't know how to fix it.
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numberOfButtons;
int numberOfClicks;
cin >> numberOfButtons;
cin >> numberOfClicks;
int buttonCoordinate;
vector <vector<int> > Buttons;
for (unsigned int i = 0; i < numberOfButtons; i++) {
vector<int> oneButtonCoordinate;
for (unsigned int j = 0; j < 4; j++) {
cin >> buttonCoordinate;
oneButtonCoordinate.push_back(buttonCoordinate);
}
Buttons.push_back(oneButtonCoordinate);
}
int XYCoordinate;
vector <vector<int> > coordinateList;
vector <int> clickCount;
for (unsigned int i = 0; i < numberOfClicks; i++) {
vector<int> oneClickCoordinate;
for (unsigned int j = 0; j < 2; j++) {
cin >> XYCoordinate;
oneClickCoordinate.push_back(XYCoordinate);
}
coordinateList.push_back(oneClickCoordinate);
}
for (unsigned int i = 0; i < numberOfButtons; i++) {
clickCount.push_back(0);
}
for (unsigned int j = 0; j < numberOfClicks; j++) {
for (unsigned int i = Buttons.size() - 1; i >= 0; i--) {
if ((coordinateList[j][0] >= Buttons[i][0]) && (coordinateList[j][0] <= Buttons[i][1]) && (coordinateList[j][1] >= Buttons[i][2]) && (coordinateList[j][1] <= Buttons[i][3])) {
clickCount.at(i) += 1;
break;
}
}
}
(skip)
return 0;
}
Example of correct Input and Output
Input
2 5
1 5 1 5
3 8 3 8
1 1
3 3
3 5
8 8
3 10
Output
Button: #1: 1
Button: #2: 3