# How does a vector work in C/C++

CC++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand how vectors work in C/C++.

A vector data structure is an enhancement over the standard arrays. Unlike arrays, which have their size fixed when they are defined; vectors can be resized easily according to the requirement of the user.

This provides flexibility and reduces the time requirement with arrays to copy the previous elements to the newly created array.

## Example

Live Demo

#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> myvector{ 1, 2, 3, 5 };
myvector.push_back(8);
//not vector becomes 1, 2, 3, 5, 8
for (auto x : myvector)
cout << x << " ";
}

## Output

1 2 3 5 8
Published on 25-Feb-2020 07:18:40