A vector is a container in the C++ Standard Library (a bunch of stuff which sort of comes “bundled” with C++) which is essentially just an array that can grow and shrink in size. These things have been highly optimized and tried and tested for several years, and as such are generally considered a standard when creating C++ applications.

Vector Constructors.

The available constructors for a vector are given by:

1	vector<int> testVector;
2	vector<long> testVector(10);
3	vector<float> testVector(5,1.0);

 

The first syntax declares an empty vector capable of storing the integer datatype. The second declares a vector with storage space for 10 long integers, each of which is initialized to the default value for the type. The final line declares a vector with storage for 5 floats, and initializes each of their values to 1.0. Any valid type can be used for any of the constructors.

There is also a copy constructor for the std::vector class. The following code creates a vectors of integers with 10 copies of the number 5, and duplicates the vector into a new one using the copy constructor:

01	#include <iostream>
02	#include <vector>
03	 
04	using namespace std;
05	 
06	int main(int argc, char** argv) {
07	     
08	    vector<int> vectorOne(10,5);
09	     
10	    vector<int> vectorTwo(vectorOne);
11	     
12	    return EXIT_SUCCESS;
13	}

 

Accessing Elements of a Vector.

There are a number of ways to access the elements of a vector. For the moment, I will focus on two of them, one safe and one unsafe. And as a reminder, C++ vectors (and other STL containers), like raw C/C++ arrays, are accessed with indices starting at zero. This means that the first element is at position 0 in the vector, and the last element is at position (number of elements)-1.

The vector class contains a member function at() for accessing individual elements of a vector. This is the safe way of accessing elements, since attempting to access an element beyond the valid range will cause an exception to be thrown. However, the raw data stored in the vector can still be accessed using the usual [] operator, just like in a raw array. Unfortunately, just like with a raw array of data, overrunning the end of the vector using the [] operator can cause weird and unexpected things to occur, such as program crashes or unexpected results. It may also return garbage data that follows the meaningful data of the vector, which has the potential to be disastrous if it is used in subsequent operations. The following two code snippets demonstrate each of these access methods:

Safe access version:

#include <iostream>
#include <vector>
using namespace std;
 

int main(int argc, char** argv) {
     
    /*  Initialize vector of 10 copies of the integer 5 */

    vector<int> vectorOne(10,5);
     
    /*  Display size of vector */

    cout << "Size of vector is " << vectorOne.size() << " elements." << endl;
     
    /*  run through the vector and display each element, using size() to determine index boundary */

    for (long index=0; index<(long)vectorOne.size(); ++index) {

        cout << "Element " << index << ": " << vectorOne.at(index) << endl;
    }

    return EXIT_SUCCESS;
}

 

Unsafe access version

#include <iostream>
#include <vector>
 
using namespace std;

 
int main(int argc, char** argv) {
     

    /*  Initialize vector of 10 copies of the integer 5 */
    vector<int> vectorOne(10,5);
     

    /*  run through the vector and display each element, if possible */

    for (int index=0; index<20; ++index) {

        cout << vectorOne[index] << endl;

    }
    return EXIT_SUCCESS;
}

 

Sources:

http://www.cplusplus.com/reference/vector/vector/vector/

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

-The Admin.

CC BY 4.0 Creation and use of Vectors in C++ by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.