mardi 19 janvier 2021

terminate called after throwing an instance of 'std::out_of_range' in C++

I am new to c++ and learning Data structure, in the below code i am getting out of range warning do not understand what I am doing wrong please someone help.

#include <iostream>
#include <vector>
using namespace std;

int main()
{

    vector<int> numbers{100,-1,2,4,55,78,3};
    int temp {};
    int pass {};
    pass = numbers.size();
    for(int i {0} ;i<pass-1;i++){    
     for(int j {0} ; j<pass-1-i ; j++){
        if(numbers.at(j) > numbers.at(j+1)){
            temp = numbers.at(j);
            numbers.at(j)=numbers.at(j+1);
            numbers.at(j+1)=temp;
        }   
    }   
    
    }

    cout << numbers.at(0) << endl;
    cout << numbers.at(1) << endl;
    cout << numbers.at(2) << endl;
    cout << numbers.at(3) << endl;
    cout << numbers.at(4) << endl;
    cout << numbers.at(5) << endl;
    cout << numbers.at(6) << endl;
    cout << numbers.at(7) << endl;
    cout << numbers.at(8) << endl;
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire