In this C++ tutorial, you will learn how to create a vector of specific size during vector declaration, with example programs.
Create Vector with Specific Size in C++
To create a vector with specific initial size, specify the size in the vector declaration.
Syntax
The syntax to create a vector of specific initial size size and datetype type is
</>
                        Copy
                        vector<type> vectorname(size);Examples
1. Create integer vector of size=5
In the following program, we create an integer vector of size 5.
main.cpp
</>
                        Copy
                        #include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> nums(5);
    cout << "Vector size : " << nums.size() << endl;
}Output
Vector size : 52. Create string vector of size=10
In the following program, we create a string vector of size 10.
main.cpp
</>
                        Copy
                        #include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<string> names(10);
    cout << "Vector size : " << names.size() << endl;
}Output
Vector size : 10Conclusion
In this C++ Tutorial, we have learnt how to create a vector of specific initial size in C++.
