How to remove from the end of the vector?

This is a discussion on How to remove from the end of the vector? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone.
I dont have a lot of experience with STL, so here i am.
I am implementing a certain ...

How to remove from the end of the vector?

Hi everyone.
I dont have a lot of experience with STL, so here i am.

I am implementing a certain queue-like behaiviour data structure.
So i have a certain vector, to which i am adding elements via push_back.
Let's say i pushed 1,2,3,4,5 in this order.
So i have 1 2 3 4 5 stored in the vector where 1 is at myVec.begin() position.

Now i want to start removing elements from the front.

Thus after myVec.erase(myVec.begin()) i have the following:

2 3 4 5 5.

As u see, 1 is removed, everything is shifted to the front by 1.
However, i have that left over of 5. And i dont know what to do with that.
Basically what i need is for the vector to shrink as i remove the elements.
So that after myVec.erase(myVec.begin()) i have 2 3 4 5, where 2 is at myVec.begin() position and 5 is at myVec.end() position, and of course with the correct size.

This will allow you to pull out the element at position x which goes from the first to last element. Then do whatever you want with that value. When ready to erase and shrink the vector use myVec.erase(x) where x is the position of the elemt to erase. This will do what you want if in the loop like I used.

So, now my question is: How can I "properly" use iterators after performing some pushes and pops on the vector. In this case it seems my iterator must be somehow updated I assume. Like I said, i am new to STL, and probably not using iterators in a correct way.

I dont' know if my understanding of iterators is correct, but they way i see it, it is just like a pointer. Is it not?

In this case it seems my iterator must be somehow updated I assume.

Which iterator?

The temp iterator. Because in my pop method i erased the front. Everything got shifted to the front by 1, and size was automatically re-adjusted. However, the temp iterator in the print method was still going beyond the new end. Oh....i think i understand it now: Eventhough the vector released that previous end after i execute

Code:

S.erase(S.begin());

the memory is still there. So that when i increment my iterator it just goes to that memory address not officialy used by my vector. Cause they way i saw it, was that if i increment an iterator and it goes out of the boundary of my container or whatever, it automatically gets NULL'ed. I think i understand my mistake now.

That is an example of how you iterate over a vector. You don't need the size() and you don't need the i counter to iterate over the container (although you probably do want to keep i around for your output message).

>> think i understand my mistake now.
Yes, it sounds like you understand what happened.

>> S.erase(S.begin());
If you will be erasing from the front a lot, you should use deque instead of vector. The interface is practically the same, and when you call pop_front() to erase the first element of a deque it is much more efficient. Obviously, the STL has a queue and a priority_queue container, but I'm assuming you are trying to implement this on your own. By default, the standard library queue container is implemented with a deque for the same reasons I'm suggesting you consider switching to deque.

When S.empty() is true, you return from your function. So you previously declared two variables you never used as well as one function, i.e. S.begin()? Now look at the rewrite of your function that I posted. The variables are not declared and the function is not called unless they are actually going to be used.

Cause they way i saw it, was that if i increment an iterator and it goes out of the boundary of my container or whatever, it automatically gets NULL'ed.

When you got out of bounds, behavior is undefined, so expecting anything is incorrect. Your old value could be there or the operating system could have used the memory for some other value--although I suspect the vector does not release that memory back to the operating system, so your old value will always be there.

A vector doubles its size when it runs out of room to store the elements you add. That way it's more efficient. Instead of having to internally create a bigger array with 'new' every time you add something to a vector, a vector doubles its size when it runs out of room. Therefore the time consuming process of having to use 'new' to create a bigger array and then having to copy everything from the old array to the new bigger array happens less frequently. (The size() of a vector is how many elements are currently in the vector's dynamically created array. The capacity() tells you how big the internal array is.)

As a result, I doubt a vector ever voluntarily uses new to create a smaller array and then copies elements from a bigger array over to a smaller array. By nature, a vector is perfectly happy maintaining an internal array that is bigger than currently needed.