samedi 30 juillet 2016

Mathematically same condition statements, different results

I was trying to solve a coding problem that can be found here.

I tried these two definitions for my For loop

for(i=0;i+(num*len)-1<A.size();i++)

and

for(i=0;i<A.size()-(num*len)+1;i++)

Later in the code, I'm using the string.substr function to obtain substrings. For some reason, the 1st form works fine, but the second one throws the following error.

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Aborted

Can someone explain to me why this is happening? I'm using C++11 with GCC 4.8.

Aucun commentaire:

Enregistrer un commentaire