Блог пользователя temp_ac

Автор temp_ac, история, 4 года назад, По-английски

I was solving a problem using C++ vector STL. But I was getting some wrong answers for my problem. Then, I started debugging my code. And after that, I noticed some weird behavior by my vector STL.

Code 1

After giving input like this : 3 5 8 10

I am getting this output : -3 0 3 8 ( why getting 0 on index 1 ? AND where is 10 ? )

And Similarly Let's see Another weird behavior ( if we don't sort the vector v AND still giving same input as of Code 1 ):

Code 2

I am getting this output : 0 3 8 10 ( why getting 0 on index 0 ? AND where is -3 ? )

Can someone explain this why I am facing this weird behavior ?

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

when u use pushback it increments the size of vector and iserts -3 at v[4] insted of v[0]..when u sort it v[4]=10..so print n+2 values instead of n+1 and u would find 10..