C++

Can You Delete a Vector in C++?

Yes! Yes, but it does not go without constraints. There are two ways of deleting a vector. Again they do not go without constraints. One way of deleting a vector is to use the destructor of the vector. In this case, all the elements are deleted, but the name of the vector is not deleted. The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope. This means that the object cannot be accessed in a nesting scope (block). A nesting scope is an outer scope (block). A nested scope is an inner scope, which is still part of the scope of interest. These two ways of deleting a vector are discussed in this article.

In order to use a vector in C++, the program should begin with:

#include <vector>

#include <iostream>

using namespace std;

Article Content

Destroying the Vector

Any object created is in some scope. The vector is created and destroyed in the main() function scope in this section of the article. The syntax to destroy a vector is:

a.~X()

where ‘a’ is the name of the vector, and X is the class name of the vector. The vector is a data structure instantiated from a class. The name of the vector class is “vector”, with all characters in lowercase. If the name of the vector is vtr, then the vector would be destroyed with,

vtr.~vector.

The following program deletes the vector:

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

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

    vtr.~vector();

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

    return 0;
}

The output is nothing, indicating that all the vector elements, except the name of the vector, have been erased. That is fine. The above output was displayed by referencing the supposed elements. What if the output is displayed using the iterator? Consider the following program:

#include <vector>

#include <iostream>

using namespace std;

 

int main()

{

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

    vector<char>::iterator it = vtr.begin();

 

    vtr.~vector();

 

    for (it = it; it != vtr.end(); it++) {

        cout << *it << ' ';

    }

    cout << endl;

 

    return 0;

}

The output is still nothing. At this stage, it is safe to really conclude that when a vector is destroyed, all its elements are destroyed, except its name.

Vector Name not Destroyed

Since the vector name is not destroyed with the destructor, the name can still be reused in the same scope. The following program illustrates this:

#include <vector>

#include <iostream>

 

using namespace std;

int main()

{

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

    vtr.~vector();

    vtr = {'F', 'G', 'H', 'I', 'J'};

    for (int i = 0; i < vtr.size(); i++) {

        cout << vtr[i] << ' ';

    }

    cout << endl;

    return 0;

}

The output is:

F G H I J

The original content of the vector had 5 characters. The 5 elements were all erased. As the vector name was reused, new 5 characters were given as content to the vector. The output showed the new content to be correct.

However, there is still a nuance. If the new content is given with the push_back() member function, the output may be incomplete, and there may be new characters in the vector. The following program illustrates this:

#include <vector>

#include <iostream>

using namespace std;

 

int main()

{

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

    vtr.~vector();

    vtr = {'V', 'W', 'X', 'Y', 'Z'};

    vtr.~vector();

    vtr.push_back('F');

    vtr.push_back('G');

    vtr.push_back('H');

    vtr.push_back('I');

    vtr.push_back('J');

    for (int i = 0; i < vtr.size(); i++) {

        cout << vtr[i] << ' ';

    }

    cout << endl;

    return 0;

}

The output is:

p ^ t e U G H I J

‘F’ is missing in the output, and there are strange characters. Initially, the vector content is given using the assignment operator. The vector is destroyed and new content assigned again with the assignment operator. The vector is destroyed again, and this time the content is given with the push_back() member function. ‘F’ is missing in the output, and there are strange characters. This needs explanation:

When a vector is destroyed, all its elements are officially erased. What happens is that the elements are simply considered not to belong to the vector with immediate effect, and their memory locations are earmarked as reusable by any other code, with immediate effect. If this scheme is not perfectly carried out internally, as with the last program above, then there will be trouble, and the kind of output obtained above may result.

const vector

When a vector declaration is preceded by const, for constant, it can still be destroyed, as explained above. The following program illustrates this:

#include <vector>

#include <iostream>

using namespace std;

int main()

{

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

 

    vtr.~vector();

 

    for (int i = 0; i < vtr.size(); i++) {

        cout << vtr[i] << ' ';

    }

    cout << endl;

 

    return 0;

}

The output is nothing. However, under this condition (const vector), no element can be erased using the erase() member function.

Using the name in a nested Scope

Destroying a vector with ~vector destroys the content (elements) but not the vector name. The name can still be used in an inner scope, which is still part of the scope of interest. The following program illustrates this:

#include <vector>

#include <iostream>

using namespace std;

int main()

{

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

 

    vtr.~vector();

 

    if (1 == 1) {

        vtr = {'K', 'L', 'M', 'N', 'O'};

        for (int i = 0; i < vtr.size(); i++)

            cout << vtr[i] << ' ';

        cout << endl;

    }

 

    return 0;

}

The output is:

K L M N O

Note: if a vector name is to be reused, it should not be re-declared.

Let go out of Scope

When any declared object goes out of its scope, it can no longer be accessed out of its scope. This means it can no longer be accessed in a nesting scope. However, it can be accessed in a nested scope. A nested scope is still part of the scope in question.

Access in and out of Scope

The following program illustrates how a vector is accessed in scope:

#include <vector>

#include <iostream>

using namespace std;

int main()

{

    if (1 == 1) {

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

            for (int i = 0; i < vtr.size(); i++)

                cout << vtr[i] << ' ';

            cout << endl;

    }

 

    return 0;

}

The output is:

A B C D E

The main() function scope nests the if-block scope. vtr declared in the if-block scope can be accessed only in the if-block scope. It cannot be accessed outside the if-block scope. It cannot be accessed outside in the main() function block that nests the if-block. The following program will not compile, as an attempt is made to access the vector outside its scope:

#include <vector>

#include <iostream>

using namespace std;

int main()

{

    if (1 == 1) {

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

            for (int i = 0; i < vtr.size(); i++)

                cout << vtr[i] << ' ';

            cout << endl;

    }

 

    cout << vtr[1] << endl;

 

    return 0;

}

If the reader tried to compile the program, an error message would have been issued.

Nested Scope

A nested scope is still part of the scope in question. The following program illustrates how a vector can be accessed in a nested scope:

#include <vector>

#include <iostream>

using namespace std;

int main()

{

    if (1 == 1) {

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

            if (1 == 1) {

                for (int i = 0; i < vtr.size(); i++)

                    cout << vtr[i] << ' ';

                cout << endl;

            }

        }

 

        return 0;

}

The output is:

A B C D E

The main() function block nests the first if-block, which nests the second if-block. The vector is declared in the first if-block. It has been accessed in the nested (inner) if-block.

The approach of letting the vector die as it goes out of scope looks preferable compared to using the destructor. When the vector goes out of scope, its name also dies. However, it is not all the time that the programmer would want the vector to die by going out of scope. So the destructor will have to be used occasionally. Both ways have their constraints.

Conclusion

One way of deleting a vector is to use the destructor of the vector. In this case, all the elements are deleted, but the name of the vector is not deleted. The second way to delete a vector is just to let it go out of scope. Normally, any non-static object declared in a scope dies when it goes out of scope. This means that the object cannot be accessed in a nesting scope (block). A nesting scope is an outer scope (block).  However, it can be accessed in a nested scope. A nested scope is an inner scope, which is still part of the scope of interest. Both ways have constraints. A vector in an inner scope does not need to be destroyed with ~vector before letting it go out of scope to die.

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.