DEV Community

CoderLegion
CoderLegion

Posted on • Edited on

How to create an Empty Vector in C++?

🎉 Before you dive into this article...

🚀 Check out our vibrant new community at CoderLegion.com!

💡 Share your knowledge, connect with like-minded developers, and grow together.

👉 Click here to join now!

While storing data in our programming the first thing what comes in mind of a programmer is array. Arrays are one dimensional as well as multi-dimensional (2D arrays …..) so arrays are best for storing data but there is a limit for arrays to store data and the programmer needs to specify size of array before storing data at time of creation of array.
Here to solve this huge problem, we use vectors. Vectors allow us to store unlimited amount of data (until our ram capacity is free). Vectors are helpful because they keep updating on storage of data and keep increasing their size. We can say that vectors manage their size themselves.
Vector is a part of STL(standard template library) which may also be called dynamic arrays.
In order to create an empty vector, first of all you need to include header file named vector. You can include that as follows:

include

Then in the function where you want to use vector, you can create is by specifying datatype of vector and vector name as follows:

vector vectorName;
The data type can be int , double, string or char and for vector name all rules for variable naming apply.
So here is a C++ program to create an empty vector of integers.

include //Include input output stream

include //Including vector header file which allows us to use vectors

using namespace std; //using standard namespace
int main(){
Vector v;
Return 0;
}
Now the vector is created. If you want to add the data in vector you can do this by using function push_back(data). So in the vector we created we will add some values.

include

include

using namespace std;
int main(){
vector v;
v.push_back(5);
v.push_back(10);
v.push_back(2);
return 0;
}
The above code will create vector and will add 5, 10 and 2 in the vector. Consider it an array where we have first element at index 0 , second at index 1 and so on.
So we have 5 at index 0 , 10 at index 1 and 2 at index 2;
In order to access data from vector, we can do it like we do in array.

include

include

using namespace std;
int main(){
vector v.push_back(5);
v.push_back(10);
v.push_back(2);
cout< cout< return 0;
}
Like this we can create and use a vector in C++.
In order to study more about vectors you can check 8thesource for more such topics

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

This shouldn't be tagged #c which is for C, not C++. This should be tagged #cpp.

Please learn to use Markdown, specifically code fences for formatting code so it's more readable.

Lastly, your code also won't even compile. You don't include the header for vector and vector is a template class and you're missing the template types. You also misspell vector as Vector once. Case matters.