taking strings from a vector

I need to take the top level string foreach iteration of a vector and output it to a program separately...I can get first word with myvector.front()...how do I remove the following word?

STL Vectors are just dynamic arrays, so treat it like you would anyother array.
do vectors have to be removed from the back..? if I wanted to keep the remaining words within the vector while the "back" word goes off to the function? do I just pop.back() while !myvector.empty()...?
Last edited on
Nothing about a vector, or any array for that matter, restricts it to being "First In Last Out". You can access any point of the array at any time as long as it is valid.

Neither the "back()" or "pop_back()" member functions do what you are describing so I will say no.

My favorite way to iterate through a vector is this:
1
2
3
4
for(int i = 0; i < MyVector.size(); i++)
{
    //Do Something
}

This has a few limitations but it keeps it simple.
If you need to push_front/pop_front as well as push_back/pop_back you could swap to use a std::deque. It is faster than std::vector for this kind of operation, though a bit slower than std::vector in other ways.

1
2
3
4
5
	while(!dq.empty())
	{
		std::cout << dq.front() << std::endl;
		dq.pop_front();
	}


will process the deque from front to back destructively.

std::deque also supports operator[], but it's internal storage is not guaranteed to be be contiguous.
Topic archived. No new replies allowed.