# What Is Vector in Data Structure With Example?

//

Heather Bennett

A vector is a dynamic data structure in computer science that allows the storage and manipulation of a collection of elements. It is also known as an array or a dynamic array. Unlike a static array, a vector can change its size during runtime, making it more flexible and efficient in certain scenarios.

Creating a Vector
To create a vector in most programming languages, you need to define the type of elements it will store. For example, in C++:

```    `vector<int> myVector;`
```

This creates an empty vector that can store integers. Similarly, we can create vectors for other data types like strings or custom objects.

Once you have created a vector, you can add elements to it using various methods. The most common method is using the “push_back” function.

```    `myVector.push_back(10);`
`myVector.push_back(20);`
`myVector.push_back(30);`
```

Accessing Elements in a Vector
To access individual elements in a vector, you can use square brackets with the index of the element. The index starts from 0.

```    `cout << myVector[0] << endl; // Output: 10`
`cout << myVector[1] << endl; // Output: 20`
`cout << myVector[2] << endl; // Output: 30`
```

Modifying Elements in a Vector
You can modify elements in a vector by assigning new values to the desired index.

```    `myVector[1] = 50;`
`cout << myVector[1] << endl; // Output: 50`
```

Size and Capacity
Vectors keep track of their size and capacity. The size represents the number of elements currently stored in the vector, while the capacity indicates the maximum number of elements it can hold before resizing. Resizing occurs automatically when necessary.

• To get the size of a vector, you can use the “size” function:
• ``cout << myVector.size() << endl;``
• To get the capacity of a vector, you can use the “capacity” function:
• ``cout << myVector.capacity() << endl;``

Iterating Over a Vector
You can iterate over a vector using loops like “for” or “while.” Here’s an example using a for loop:

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

### Example:

Let’s consider an example where we want to store and display names of different fruits in a vector.

``````// Include necessary libraries
#include<iostream>
#include<vector>

using namespace std;

int main() {
// Create a vector to store fruit names
vector<string> fruits;

// Add elements to the vector
fruits.push_back("Apple");
fruits.push_back("Banana");
fruits.push_back("Orange");

// Display all elements in the vector
for(int i = 0; i < fruits.size(); i++) {
cout << fruits[i] << endl;
}

return 0;
}
``````

When we run this code, it will output:

```Apple
Banana
Orange
```

In this example, we created a vector of strings and added three fruit names to it. Then, using a for loop, we displayed each fruit name on a new line.

Vectors provide a powerful way to store and manipulate collections of data in various programming languages. Understanding their usage and functionality can greatly enhance your ability to write efficient and robust code.