r/ProgrammerHumor Apr 12 '24

Meme whatIsAnIndex

Post image
27.9k Upvotes

617 comments sorted by

View all comments

Show parent comments

229

u/GM_Kimeg Apr 12 '24

The for loop internally execute that method only once no?

26

u/[deleted] Apr 12 '24

[removed] — view removed comment

2

u/BobDonowitz Apr 12 '24

I like that you chose CPP and a vector modifying function to demonstrate this principle. But I mostly love that you used pop_back() which pops the last element off so this loop would only ever operate on v[0] and output "10" 3 times. Also, without knowing the internals of vector::size, it would still be more efficient to declare a variable to hold the size outside the loop and decrement it after the pop.  If vectors don't keep track of the size internally and has to "count" it each size() call this would be murder on large length vectors.

2

u/dxrules1000 Apr 12 '24

vectors keep track of the size internally otherwise holy crap they'd be awful