temp_ac's blog

By temp_ac, history, 4 years ago, In English

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 ?

  • Vote: I like it
  • -6
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it +8 Vote: I do not like it

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..