1. ## STL vectors

I'm a novice when it comes to the STL. What I need to do is remove an element from anywhere in the list but it seems like I can only use pop_back() to remove the end element. The only way I can think of doing it is swapping the end element with the element I want to delete and then call pop_back() but there must be an easier way right?

2. Sure, use vector::erase. The annoying thing is that you have to use iterators . Here's a reference:http://msdn2.microsoft.com/en-us/library/ceh559x2.aspx

3. if youre using vectors then this is the best solution if the ordering of your elements don't matter.

hm... if im not mistaken (and i think im not )erase shifts all elements with an index greater than the index of the element to erase down by 1.
so if you quickly want to remove an element, use the swapping method

[/edit]

4. Here is an example:
Code:
```#include <vector>
#include <algorithm>  // for find_if
#include <iterator>
#include <iostream>
using namespace std;

int main(void)
{
vector<int> vec;

for (int i = 0; i < 10; i++)
{
vec.push_back(i);
}

cout << endl;
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));

vec.erase(vec.begin() + 5);
vec.erase(vec.begin() + 7);

cout << endl;
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));

/* Use the swap method to delete vec[1]. */
iter_swap(vec.begin() + 1, vec.end() - 1);
vec.erase(vec.end() - 1);

cout << endl;
copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));

cin.get();
}```
Output:
Code:
```0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 6 7 9
0 9 2 3 4 6 7```
Here is the STL documentation.

5. What I need to do is remove an element from anywhere in the list but it seems like I can only use pop_back() to remove the end element
A vector is not referred to as a list. The reason for that is that there is another STL container called a <list>, which is a linked list. A list is actually what you should be using instead of a vector. Erasing an element in a vector, means the vector has to take all the elements to the right of the removed element and shuffle them to the left in order to fill the gap. That is inefficient.

If you erase an element in a linked list, the element is detached from the two elements to the left and the right, and then the left and right elements are reattached to each other. That is more efficient than shuffling over all the elements to the right of the erased element to fill the gap.

6. Thanks for all the info. I wasn't aware of list however I was going to write my own linked list but I didn't because I thought vectors will do the job. I will try the list approach.

one problem: vector seems to be the only one that uses [] operators to access elements...

7. Deque and map use [] as well. Deque is slight improvement over vector because you can push/pop_front().

8. Ok it's all done. I used vector and it works fine thanks all.