The C++ vector Class

C++ contains a generic vector class for storing a list of objects.

You can use an array to store a collection of data such as strings and int values. There is a serious limitation: The array size is fixed when the array is created. C++ provides the vector class, which is more flexible than arrays. You can use a vector object just like an array, but a vector’s size can grow automatically if needed.

To create a vector, use the syntax:

vector<e1ementType> vectorName;

For example,

vector<int> intVector;

creates a vector to store int values.

vector<string> stringVector;

creates a vector to store string objects.

Figure 12.2 lists several frequently used functions in the vector class in a UML class diagram.

You can also create a vector with the initial size, filled with default values. For example, the following code creates a vector of initial size 10 with default values 0.

vector<int> intVector(10);

A vector can be accessed using the array subscript operator []. For example,

cout << intVector[0];

displays the first element in the vector.

Listing 12.8 gives an example of using vectors.

Listing 12.8 TestVector.cpp

1 #include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
7 {
8     vector<
int> intVector;
// Store numbers 1, 2, 3, 4, 5, …, 10 to the vector
11    for (int i = 0; i < 10; i++)
12    intVector.push_back(i +
// Display the numbers in the vector
15    cout << “Numbers in the vector: “;
for (int i = 0; i < intVector.size(); i++)
17    cout << intVector[i] <<
” “;

19    vector<string> stringVector;
// Store strings into the vector
22    stringVector.push_back(“Dallas”);
23    stringVector.push_back(
24    stringVector.push_back(
25    stringVector.push_back(
// Display the string in the vector
28    cout << “\nStrings in the string vector: “;
for (int i = 0; i < stringVector.size(); i++)
30    cout << stringVector[i] <<
” “;
32    stringVector.pop_back();
// Remove the last element
34    vector<string> v2;
35    v2.swap(stringVector);
36    v2[
0] = “Atlanta”;
// Redisplay the string in the vector
39    cout << “\nStrings in the vector v2: “;
for (int i = 0; i < v2.size(); i++)
41    cout << <<
” “;
return 0;
44 }

Since the vector class is used in the program, line 2 includes its header file. Since the string class is also used, line 3 includes the string class header file.

A vector for storing int values is created in line 8. The int values are appended to the vector in line 12. There is no limit on the size of the vector. The size grows automatically as more elements are added into the vector. The program displays all the int values in the vector in lines 15-17. Note the array subscript operator [] is used to retrieve an element in line 17.

A vector for storing strings is created in line 19. Four strings are added to the vector (lines 22-25). The program displays all the strings in the vector in lines 29-30. Note the array sub­script operator [] is used to retrieve an element in line 30.

Line 32 removes the last string from the vector. Line 34 creates another vector v2. Line 35 swaps v2 with stringVector. Line 36 assigns a new string to v2[0]. The program displays the strings in v2 (lines 40-41). Note that the at function is used to retrieve the elements. You can also use the subscript operator [] to retrieve the elements.

The size() function returns the size of the vector as an unsigned (i.e., unsigned integer), not int. Some compilers may warn you because an unsigned value is used with a signed int value in variable i (lines 16, 29, 40). This is just a warning and should not cause any prob­lems, because the unsigned value is automatically promoted to a signed value in this case. To get rid of the warning, declare i to be unsigned int in line 16 as follows:

for (unsigned i = 0; i < intVector.size(); i++)

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *