In this C++ tutorial, you will learn how to create an empty vector by declaring a vector with elements of specific type, with example programs.

Create an Empty Vector in C++

To create an empty vector in C++, just declare the vector with the type and vector name.

Syntax

The syntax to create an empty vector of datatype type is

vector<type> vectorname;

For example, to create an empty vector nums of type int , the code is

vector<int> nums;

Examples

1. Empty integer vector

In the following program, we create an empty vector of type int. Also, we will print out the length of this vector. Since, the vector is empty, it must have a length of 0.

main.cpp

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

int main() {
    vector<int> nums;
    cout << "Vector Length : " << nums.size() << endl;
}

Output

Vector Length : 0

2. Empty string vector

In the following program, we create an empty vector of type string.

main.cpp

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

int main() {
    vector<string> names;
    cout << "Vector Length : " << names.size() << endl;
}

Output

Vector Length : 0

Conclusion

In this C++ Tutorial, we have learnt how to create an empty vector of specific type in C++.