The Vector Container in C++
Container Library Sequences in C++ Simplified – Part 4
Forward: In this part of the series, we begin to look at a sequence container in C++ called, vector.
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.
Differences between the Sequences
The three sequences I will treat in this series each has its own strong feature against the others. The vector is a general-purpose sequence container. The list should be used when there are frequent insertions and deletions from the middle of the sequence. The deque should be used when most insertions and deletions take place at the beginning or at the end of the sequence.
Header File for vector Class Template
In order to use the vector Class Template you need to include the header file, vector.
Properties of the vector
The vector is a class and so it has properties. A property is an identifier with an object type. For a vector, many of the property object types are other classes. A vector also has methods. Every container has a list; so the vector has a list. The methods operate on the list. Their return values are the vector property types. The names of the vector properties are:
Reference
const_reference
iterator
const_iterator
size_type
difference_type
value_type
allocator_type
pointer
const_pointer
reverse_iterator
const_reverse_iterator
The name of each of these properties, gives you an idea of what it is. To fully understand each of these properties, you need a whole article or a whole series. As I go on to explain the other features of the vector, you will know the uses of these properties. If you have read the prerequisite I gave in the first part of the series, you should be alright as we go along.
It is the constructor function of any class that instantiates an object for that class. I will give you two ways of instantiating a vector. In simple terms, the syntax of one way is as follows:
vector<T> vectorName:
Here, T means the type of values (objects) you want for each of the elements in the list. The following program is an illustration:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myVtor;
return 0;
}
The second line in this code includes the vector. You cannot work with vectors without this header file. In main, the first statement begins with the word, vector, which is the class. Next to this class name, we have int in angle brackets. int is the particular object type we want for each of the elements of the vector list. int in this position is called a template argument. After that on the same line, you have the name we give to the instantiated vector. The name is myVtor. With is statement, a vector is instantiated.
Any instantiated vector has a list. In this case the list is empty. We shall see how to fit in elements later.
In simple terms, the syntax of another way to instantiate a vector is:
vector<T> vectorName(size_type n, T obj):
Here, you can consider size_type as an int. The first parameter is the number of initial elements that you want. The second is the initial value you want for each of the elements in the list. The second parameter is optional. The following program illustrates the syntax:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myVtor(5, 2);
return 0;
}
The difference between this program and the previous, is the presence of arguments in the constructor call, of the first statement in main. We now have a vector list of 5 elements where each value of the element is 2. We shall see how to read the elements later.
The vector destructor is,
˜vector();
We have seen two ways of instantiating a vector. You will use these ways often. If you want to know the few other ways then you have to consult some other document. We take a break and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT