Core C++

Lesson 50 : Simple STL Vector Usage


In this C++ video tutorial, we demonstrate the basics of using STL vectors. A vector is a type of container in the STL. Containters are use to hold a collection of objects of the same type. Along with a container, we use iterators to access the items in a container. Both containers and iterators are class templates, so they requires type parameters to specify how the classes are instantiated.

#include <iostream>
#include <vector>
#include <string>

int main()
{
    using namespace std;

    vector<string> qV;
    qV.push_back("XoaX.net");
    qV.push_back("C++");
    qV.push_back("Video");
    qV.push_back("Tutorials");

    vector<string>::iterator qIter;
    for (qIter = qV.begin(); qIter != qV.end(); ++qIter) {
        cout << *qIter << " ";
    }
    cout << endl;

    cin.get();
    return 0;
}

Typically, we will want to step through all of the items in a container an access each one, as shown in the for-loop of the program above. Note the syntax. The vector is a vector of strings called "qV," and the iterator is called "qIter." Both the iterator and the vector are parameterized, using the string type. These types must be consistent in the parameters type that they use, inorder to work together.

The begin() and end() function specify the start and the end of the vector. While begin() gives us the first item in the vector, the end() function actually returns the location after the last item in the vector. This is done to make the syntax easier for this and other operations on the vector entries. Moreover, the iterator acts like a pointer, and we use the dereference operator, just as we do here, to access the item that the iterator points to.

#include <iostream>
#include <vector>
#include <string>

int main()
{
    using namespace std;

    vector<string> qV;
    qV.push_back("XoaX.net");
    qV.push_back("C++");
    qV.push_back("Video");
    qV.push_back("Tutorials");

    unsigned int uiIndex;
    for (uiIndex = 0; uiIndex < qV.size(); ++uiIndex) {
        cout << qV[uiIndex] << " ";
    }
    cout << endl;

    cin.get();
    return 0;
}

Iterators are useful for generic container access, but we can use the simpler bracket operator and integer indices to access elements in a vector, as we demostrate in the program above. This code and the program before it generate the same output, which is shown below:

An STL vector is like an array in that we can randomly access elements via the bracket operator and an integer index. So, it seems reasonable to ask why we use iterators. The answer is that iterators allow use create generic algorithms that can run on any container. For this reason, most vector functions use iterators rather than indices. The function calls below, for example, can be used to insert an remove items from a vector. Notice that the first argument passed into insert() and both arguments for erase() are iterators.

qV.insert(qV.begin() + 2, "Console");
qV.erase(qV.begin() + 1, qV.begin() + 3);
 

© 2007–2024 XoaX.net LLC. All rights reserved.