In order to remove a specific element from the C++ vector, the element has to be identified. Concerning the erase() member function, the element is identified by an iterator. The next question is, “What is an iterator?” – see below. Since the pop_back() member function removes the last element by definition, the specific element it has to remove has already been identified indirectly.
To use the vector library in C++, the program has to begin with:
#include <vector>
using namespace std;
This article explains how to remove a specific element from the C++ vector, beginning with explaining the principal iterator in C++. All vector code for this article is in the main() function body.
Article Content
Identifying Vector Element
Identifying by Reference
Consider the following vector:
vtr[0] returns ‘A’. vtr[1] returns ‘B’. vtr[2] returns ‘C’. vtr[3] returns ‘D’. vtr[4] returns ‘E’. This is identifying by reference. The number in square brackets, is called an index. It can be done in a for-loop, as the following program shows:
#include <vector>
#include <string>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
for (int i=0; i<vtr.size(); i++)
cout << vtr[i] << ' ';
cout << endl;
return 0;
}
The output is:
Identifying by Iterator
Consider the following vector:
it = vtr.begin(), means ‘it’ is an iterator that points to ‘A’ .
it++ points to ‘B’.
it++ points to ‘C’, incremented after it was pointing to ‘B’.
it++ points to ‘D’, incremented after it was pointing to ‘C’.
it++ points to ‘E’, incremented after it was pointing to ‘D’.
it++ points to vtr.end(), which is just beyond the last element, incremented after pointing to ‘E’.
This can be done in a for-loop, as the following program shows:
#include <vector>
#include <string>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
for (vector::iterator it=vtr.begin(); it != vtr.end(); it++)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
The first iterator was gotten with the declaration:
where ‘it’ is the identifier of the iterator.
Still referring to the same vector,
it = vtr.end(), points just beyond the last element ‘E’ .
it– points to ‘E’.
it– points to ‘D’, decremented after it was pointing to ‘E’.
it– points to ‘C’, decremented after it was pointing to ‘D’.
it– points to ‘B’, decremented after it was pointing to ‘C’.
it– points to ‘A’, decremented after it was pointing to ‘B’.
This scheme can be used to display the vector in reverse order, as the following program shows:
#include <vector>
#include <string>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
vector::iterator it=vtr.end();
for (it = --it; it >= vtr.begin(); it--)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
The initial iterator for the for-loop is decremented. i.e. “it = –it;”. Note the while-condition for the for-loop, i.e. “it >= vtr.begin();”.
To obtain the value pointed to by the iterator, the iterator has to be dereferenced, by the indirection operator, *.
Removing with erase()
The syntax to erase (remove) an element from a vector is:
where ‘a’ is the vector’s name, and q is an iterator pointing to the element to be removed. That is, q identifies the specific element to be removed. The member function returns an iterator that points to the element just after the one is removed.
Removing from the Front
The erase member function can remove an element from the front of the vector. The following program illustrates this:
#include <vector>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
vector::iterator q = vtr.begin();
vector::iterator p = vtr.erase(q);
cout << *p << endl;
for (int i=0; i < vtr.size(); i++)
cout << vtr[i] << ' ';
cout << endl;
for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
B C D E
B C D E
The first two lines in the program include directives, including the necessary libraries. The next line declares that any name used is from the standard namespace unless otherwise indicated.
In the main() function, the first statement declares the vector. The statement after returns an iterator, q, that points to the first element of the vector. The statement that follows is the statement of interest. It removes the first element, which is pointed to by q. It returns an iterator that points to the element, which was just after the element was removed. The statement after prints the value of the element the iterator is pointing to after removal. The next two code segments display the remaining values in the vector. The first of these code segments use references. The second uses iterators.
Removing from within
In order to remove the element of value, ‘C’, the iterator returned, by begin() has to be incremented two times to point to the element of value, ‘C’. The erase() member function can then use the iterator to remove the element. The following program illustrates this:
#include <vector>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
vector::iterator q = vtr.begin();
q++; q++;
vector::iterator p = vtr.erase(q);
cout << *p << endl;
for (int i=0; i < vtr.size(); i++)
cout << vtr[i] << ' ';
cout << endl;
for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
A B D E
A B D E
Removing from the Back with erase()
In order to remove the element of value, ‘E’, the iterator returned, by the end() has to be decremented once, to point to the element of value, ‘E’. The erase() member function can then use the iterator to remove the element. The following program illustrates this:
#include <vector>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
vector::iterator q = vtr.end();
q--;
vector::iterator p = vtr.erase(q);
cout << *p << endl;
for (int i=0; i < vtr.size(); i++)
cout << vtr[i] << ' ';
cout << endl;
for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
A B C D
A B C D
There is a nuance here. The iterator returned by end() points to the element, which should have been there, had it not been removed.
Removing with pop_back
The pop_back() member function removes the last element of the vector, by definition. So the programmer does not need to identify the last element. Its syntax is
where ‘a’ is the name of the vector. It takes no argument; it returns void. The following program removes the last element of a vector:
#include <vector>
using namespace std;
int main(){
vector vtr = {'A', 'B', 'C', 'D', 'E'};
vtr.pop_back();
for (int i=0; i < vtr.size(); i++)
cout << vtr[i] << ' ';
cout << endl;
for (vector::iterator it = vtr.begin(); it != vtr.end(); it++)
cout << *it << ' ';
cout << endl;
return 0;
}
The output is:
A B C D
Conclusion
The C++ vector has many member functions. Two of these member functions are erase() and pop_back(). pop_back() removes the last element from the vector. In order to remove all the elements from the vector, using pop_back(), the pop_back() function has to be repeated the number of times there are elements. The erase() function can remove an element from the beginning, within, or end of the vector. In order to remove all the elements from the vector, using erase(), the erase() function has to be repeated the number of times there are elements, beginning from the first element.
In order to remove a specific element from the C++ vector, the element has to be identified. For the erase() member function, the element is identified by an iterator. Since the pop_back() member function removes the last element by definition, the specific element it has to remove has already been identified indirectly.