'C++ Vector returning odd output for last iteration?

I am attempting to figure out the Skyline problem to better understand C++, right now I am learning more about how I can use vectors. I am trying to iterate through to return the value of each vector, and the first 3 values are correct, but there is a huge negative number that gets outputted at the end. Why?

class Buildings{
public: 
    int startX; 
    int width; 
    int height; 
    vector<int> xData; 
    vector<int> heightData; 
    vector<int> EndXData; 

    // build rectangles 

    // findX is used to break appart the input by a count of 3 (0, 3, 6) to find X start

    findData(int buildingNumber){
            for(int i = 0; i <= inputData.size(); i = i+3){
                if(buildingNumber + 1 > inputData.size()/3) {
                    cout << "invalid" << endl; 
                    break; 
                } else {
                xData.push_back(inputData[i]); 
                heightData.push_back(inputData[i+1]);
                EndXData.push_back(inputData[i+2]);
            }
        }
    };

    void print(){
        for(int i=0; i<xData.size(); i++){
            cout << xData.at(i) << endl; 
        }
    }; 
}; 

int main(){
    Buildings skyscraper;
    skyscraper.findData(2);  
    skyscraper.print();  
    return 0; 
} 

Output: 1 3 2 -2021227132



Solution 1:[1]

You need to replace the following line:

for(int i = 0; i <= inputData.size(); i = i+3){

With

for(int i = 0; i < inputData.size(); i = i+3){

(i.e. replace <= with < ).

The reason is that elements in an array/vector have indiced of 0..(n-1), where n is the number of elements.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1