c++ - terminate called after throwing an instance of 'std::out_of_range' -
why happen program says has no errors when run terminate called after throwing instance of 'std::out_of_range' what(): vector:_m_range_check. new c++ don't understand these errors
#include <vector> #include <iostream> #include <random> #include <time.h> using namespace std; using std::vector; int main() { vector<int> deck; vector<int> nums; default_random_engine eng(time(0)); uniform_int_distribution<int> dis(0, 51); int pos1; int pos2; int num1; int num2; int i; int n; int m; (i = 0; < 52; i++) { nums.push_back(i); } for(int j = 0; j < 52; j++) { cout << nums.at(i) << "\n"; } for(n = 0; n < 50; n++) { pos1 = dis(eng); pos2 = dis(eng); cout << pos1 << "\n" << pos2 << "\n"; num1 = deck.at(pos1); num2 = deck.at(pos2); } }
it looks me if due typo, , should use variable 'j' in second loop. after first loop,
for (i = 0; < 52; i++) { nums.push_back(i); }
the variable 'i' contains value 52, sounds expected calling nums.at(i) throw std::out_of_range, since nums contains 52 values, starting @ index 0.
for(int j = 0; j < 52; j++) { cout << nums.at(i) << "\n"; }
fix replacing argument of at() 'j', assume original intent:
for(int j = 0; j < 52; j++) { cout << nums.at(j) << "\n"; }
Comments
Post a Comment