C++

What is the difference between a Vector and an Array in C++?

There are many differences between a vector and an array in C++. However, the main similarities are very important. The main similarities are that they are both a list, and each would hold a sequence of data of the same type. The main differences are as follows: A vector's size (length) can be increased naturally, but that of an array is fixed and cannot be increased. Elements can be inserted within a vector but cannot be inserted within an array. Elements can be appended at the end of the vector but cannot be appended at the end of the array. The vector is a class from which other vector objects are instantiated, but the array is a constant pointer to a data sequence of the same type. The vector has methods (member functions), but the array does not, and so the vector is called a data structure. While the pointer can be used with the array, iterators are used with the vector. An iterator is an elaborated pointer.

No element can be included in front of the array. With C++ 17 and higher, an element can be included in front of the vector using the emplace() member function.

For the rest of this article, differences between the vector and the array are illustrated. For each point, the incapacity of the array is mentioned, or its blunt or cumbersome way of achieving the same goal is given.

Article Content

Creating a Vector or Array

A vector can be created in several ways. The basic way is as follows:

vector<char> vtr = {'A', 'B', 'C', 'D','E'};

Correspondingly, an array would be created as follows:

char arr[] = {'A', 'B', 'C', 'D','E'};

Note the difference in the operands that are on the left of the assignment operator. The number of elements for the vector can then be added or reduced, but the size of the array stays fixed, at 5 in this case.

To have and use a vector in a program, the program should begin with:

#include <vector>

using namespace std;

To have and use an array in a program, no preprocessor directive is needed.

Increasing Size

The following code shows how a vector of initially two elements, is increased to four elements, using its push_back() member function:

  vector<char> vtr(2);

vtr[0] = 'A';

vtr[1] = 'B';

vtr.push_back('C');

vtr.push_back('D');

This code should be in a function body. For the array, and since the array has a fixed size, create an array for the maximum number of elements envisaged, before adding the elements using the [] operator. Example:

  char arr[4];

arr[0] = 'A';

arr[1] = 'B';

  //adding elements

arr[2] = 'C';

arr[3] = 'D';

Also, this code should be inside a function body.

Inserting

In the following code, an element is inserted in front of the element pointed to by the iterator, p:

    vectorvtr = {'A', 'B', 'D', 'E'};
    vector::iterator p = vtr.begin();
    ++p;
    ++p;
    char ch = 'C';
vtr.insert(p, ch);

    for (int i=0; i<vtr.size(); i++) {
cout<<vtr[i] << ' ';
    }

The output is:

A B C D E

The first statement of the code creates the vector object. 'C', which should have been in front of 'D' by alphabetical order, is absent here. The second statement returns an iterator that points to the first element of the vector. The next two statements increment the pointer to point to 'D'. The statement after assigns' C' to ch. In that code segment, the last statement inserts 'C' in front of 'D', using the iterator.

As for the array, there is no way an element can be inserted. Because of limitations like this for the array, the vector and other containers were designed.

Note: The insert() member function can also be used to insert an element in front of a vector.

Appending

Appending means adding elements at the back. The push_back() member function can be used to add elements at the back of the vector – see above. The array cannot be appended to. The only way to work around this problem for the array is to create an array for the maximum size envisaged. Put in elements from the beginning. Then some space (cells) will be left behind in the array. Then if there is any need to add elements at the back, fit the elements (values) in the spaces behind that are empty (that have default values).

Erasing an Element

For the vector, an element can be erased using the iterator. The iterator will then point to the next element, which was there before the erasing took place. The following code erases ‘B’:

    vectorvtr = {'A', 'B', 'C', 'D', 'E'};
    vector::iterator q = vtr.begin();
    ++q;
vtr.erase(q);

    for (int i=0; i<vtr.size(); i++) {
cout<<vtr[i] << ' ';
    }
cout<<endl;
cout<< *q <<endl;

The output is:

A C D E

C

No element of the array can be erased, though it can be changed.

Clear

All the elements of the vector can be removed, with its member function clear(), as follows:

    vectorvtr = {'A', 'B', 'C', 'D', 'E'};
vtr.clear();

    for (int i=0; i<vtr.size(); i++) {
cout<<vtr[i] << ' ';
    }

The output is nothing. The best thing to do with the array is to replace all the elements with some default value. With the integer, the default value is 0. The following code illustrates:

    int arr[] = {1, 2, 3, 4, 5};

    for (int i=0; i<5; i++) {
arr[i] = 0;
    }

    for (int i=0; i<5; i++) {
cout<<arr[i] << ' ';
    }

The output is:

0 0 0 0 0

With the character, the default value is the nul character, \0. The following code illustrates:

    char arr[] = {'A', 'B', 'C', 'D', 'E'};

    for (int i=0; i<5; i++) {
arr[i] = '\0';
    }

    for (int i=0; i<5; i++) {
cout<<arr[i] << ' ';
    }

The output shows nothing.

Swapping

Even if two vectors are not of the same size, their elements can be swapped, with the swap() member function. The following code shows this:

    vector vtr1 = {'A', 'B', 'C', 'D', 'E'};
    vector vtr2 = {'F', 'G', 'H'};

    vtr1.swap(vtr2);

cout<< "Content of new vtr1:" <<endl;
    for (int i=0; i<vtr1.size(); i++) {
cout<< vtr1[i] << ' ';
    }
cout<<endl;
cout<< "Content of new vtr:" <<endl;
    for (int i=0; i<vtr2.size(); i++) {
cout<< vtr2[i] << ' ';
    }

For two arrays to be swapped, they must be of the same length. The array does not have member functions (no methods). So, to swap elements for arrays, a code should be written as follows:

    char arr1[] = {'A', 'B', 'C', 'D', 'E'};
    char arr2[] = {'F', 'G', 'H', 'I', 'J'};

    for (int i=0; i<5; i++) {
        char temp = arr1[i];
        arr1[i] = arr2[i];
        arr2[i] = temp;
    }

cout<< "Content of new arr1:" <<endl;
    for (int i=0; i<5; i++) {
cout<< arr1[i] << ' ';
    }
cout<<endl;
cout<< "Content of new arr2:" <<endl;
    for (int i=0; i<5; i++) {
cout<< arr2[i] << ' ';
    }

The output is:

Content of new arr1:

F G H I J

Content of new arr2:

A B C D E

Size

The size of the vector is returned by its member function, size(). That is, it is determined at run-time. Illustration:

vectorvtr = {'A', 'B', 'C', 'D'};
    int sz = vtr.size();
cout<<sz<<endl;

The output is 4. The size of the array has to be indicated at the beginning, as the following code shows:

char arr[4] = {'A', 'B', 'C', 'D'};

It can also be done like this:

char arr[7] = {'A', 'B', 'C', 'D'};

That is, putting a number (size) that is higher than the supposed size (of 4 in this case). However, the number should not be smaller than the number of initial elements.

Variable Length Array

The size of the array can, however, be given (not determined) at run-time. In this case, the array will have to be created in a function or in some similar construct. The following program illustrates this:

    #include <iostream>

    #include <vector>

    using namespace std;

    void fn(int n) {
        char arr[n];
arr[0] = 'A';
arr[1] = 'B';
arr[2] = 'C';
arr[3] = 'D';

        for (int i=0; i<n; i++) {
cout<<arr[i] << ' ';
        }
cout<<endl;
    }

    int main()
    {
fn(4);

        return 0;
    }

The output is:

A B C D

Conclusion

The main differences between the vector and the array are as follows: The size (length) of a vector can be increased naturally, but that of an array is fixed and cannot be increased. Elements can be inserted within a vector but cannot be inserted within an array. Elements can be appended at the end of the vector but cannot be appended at the end of the array. The vector is a class from which other vector objects are instantiated, but the array is a constant pointer to a data sequence of the same type. The vector has methods (member functions), but the array does not, and so the vector is called a data structure. While the pointer can be used with the array, iterators are used with the vector. An iterator is an elaborated pointer. The array either shows its incapacity or has a blunt or cumbersome way of achieving the same goal for each difference.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.