Vector Modifiers in C++
Container Library Sequences in C++ Simplified – Part 7
Forward: In this part of the series, we look at the methods that will modify a vector in C++.
By: Chrysanthus Date Published: 24 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
void push_back(const T& x);
This method adds a new element at the end of the vector list. It takes the value of the new element as argument. The value could be represented by its identifier in the argument. Remember, in the parameter, T is the type placeholder. It stands for an int or float or any other object type, including instantiated objects. If the vector list is empty, the method brings in the first element. Read and try the following code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float> myVtor;
myVtor.push_back(4.5);
myVtor.push_back(3.6);
cout << myVtor[0] << '\n';
cout << myVtor[1] << '\n';
return 0;
}
If an exception is thrown by the push_back() method, that method has no effect. If an exception thrown from a vector is not caught, your program may abort. If your vector code and your program as a whole is well written, hardly would an exception be thrown.
void pop_back();
This method takes out the last element from the vector list. It does not return anything. You can know if the element was removed by checking the size of the vector. The size of the vector list is reduced by 1 (we shall see how to check the size later). The following program, which displays nothing, shows the use of the pop_back() method.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<float> myVtor;
myVtor.push_back(4.5);
myVtor.push_back(3.6);
myVtor.pop_back();
return 0;
}
I will give you two types of the insert method.
iterator insert(iterator position, const T& x);
From the parameters, this method inserts the value x in a vector list at a position identified by an iterator. It returns an iterator of the position where the copy has been made. So the parameter iterator and the return iterator should refer to the same thing. After insertion one element, the size of the vector list is increased by one.
Note: An iterator can be incremented to refer to the following element or decremented to refer to the previous element.
Read and try the following code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(5);
myVtor[0] = 'A';
myVtor[1] = 'B';
myVtor[2] = 'C';
myVtor[3] = 'D';
myVtor[4] = 'E';
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter1 = myVtor.begin();
++iter1;
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter2 = myVtor.insert(iter1, 'Z');
cout << myVtor[1];
return 0;
}
The secret I mentioned in the previous part of the series manifests itself here as follows: The begin method returns an iterator. The insert method uses this iterator as one of its arguments to insert an element. As I promised, you do not really need to know the details of iterator in order to use it with the vector. The last but one statement above confirms that the element was inserted.
void insert(iterator position, size_type n, const T& x);
With this other insert method, there is a new parameter, which goes in between the other parameters. This new parameter is the number of elements you want to insert. The first parameter gives the start insertion position in the vector list. The last parameter is the single same object that will be inserted consecutively as new elements. This method returns nothing (void). Read and try the following code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(2);
myVtor[0] = 'A';
myVtor[1] = 'B';
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter = myVtor.begin();
++iter;
myVtor.insert(iter, 3, 'Z');
cout << myVtor[0] << '\n';
cout << myVtor[1] << '\n';
cout << myVtor[2] << '\n';
cout << myVtor[3] << '\n';
cout << myVtor[4] << '\n';
return 0;
}
The next method has two types.
This method removes an element referred to by the iterator argument. It returns the iterator of the position of the element removed. Read and try the following code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(5);
myVtor[0] = 'A';
myVtor[1] = 'B';
myVtor[2] = 'C';
myVtor[3] = 'D';
myVtor[4] = 'E';
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter1 = myVtor.end();
--iter1;
--iter1;
myVtor.erase(iter1);
cout << myVtor[3];
return 0;
}
The program uses the end method to point just after the last element. The iterator is decremented twice. The resulting iterator is now used to erase the fourth element. The last but one statement displays the new fourth element, which was the fifth element.
Even though a method or function may return a value, you do not have to use (assign) the returned value. The return value of the erase method here has not been used.
iterator erase(iterator first, iterator last);
The method can erase a range of elements in the list. It takes an iterator that refers to the first element of the range and another iterator that refers to the last element of the range. The returned iterator refers to the element that was just below the range. The following code illustrates this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(5);
myVtor[0] = 'A';
myVtor[1] = 'B';
myVtor[2] = 'C';
myVtor[3] = 'D';
myVtor[4] = 'E';
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter1 = myVtor.begin();
++iter1;
__gnu_cxx::__normal_iterator<char*, vector<char, allocator<char> > > iter2 = myVtor.end();
--iter2;
myVtor.erase(iter1, iter2);
cout << myVtor[1];
return 0;
}
The program removes the range from the second to the fourth element. The last but one statement confirms this.
This method exchanges the content (lists) of two vectors. The two vectors should be of the same type. The method takes the name of one vector as argument.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(5);
myVtor[0] = 'A';
myVtor[1] = 'B';
myVtor[2] = 'C';
myVtor[3] = 'D';
myVtor[4] = 'E';
vector<char> hisVtor(5);
hisVtor[0] = 'F';
hisVtor[1] = 'G';
hisVtor[2] = 'H';
myVtor.swap(hisVtor);
cout << myVtor[0] << '\n';
cout << myVtor[1] << '\n';
cout << myVtor[2] << '\n';
cout << '\n';
cout << hisVtor[0] << '\n';
cout << hisVtor[1] << '\n';
cout << hisVtor[2] << '\n';
cout << hisVtor[3] << '\n';
cout << hisVtor[4] << '\n';
return 0;
}
void clear();
This method removes all the elements that are in the vector list, making the size of the vector zero. The following program illustrates the use of the clear method (nothing is displayed):
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char> myVtor(5);
myVtor[0] = 'A';
myVtor[1] = 'B';
myVtor[2] = 'C';
myVtor[3] = 'D';
myVtor[4] = 'E';
myVtor.clear();
return 0;
}
In the next part of the series, we shall see how to determine the size of a vector.
We have come to the end of this part of the series. I hope with the knowledge you have gained in this part and the previous part, you are now able to work with iterators. We continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT