Weird behavior by vector STL in C++

Revision en2, by temp_ac, 2020-05-02 20:04:21

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 ?

Tags c++, vector, c++ stl

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English temp_ac 2020-05-02 20:04:21 458
en1 English temp_ac 2020-05-02 20:02:55 759 Initial revision (published)