Vector in C++

Vector in C++

Sarthak Kochhar

A vector is a collection of objects stored in contiguous memory locations. It functions as a dynamic array where elements can be inserted or deleted at any point in the collection. This dynamic nature allows for fast access time when searching for or manipulating specific elements of the vector. 

To use Vectors in C++, the “vector” header must be included in your program to access its related functions and methods. Once included, you can create and initialize a vector like any other standard container. Memory allocation occurs automatically as elements of the vector are added or removed throughout its lifetime. 

Declaration of Vector Variables

When declaring a vector variable, you need to specify the type of data the variable will store. An example of a vector declaration would look like this: 

vector<int> myVector; 

In this example, we create a vector variable called myVector that stores integer values. You can also add an initial capacity for your vector by using the below syntax: 

vector<int> myVector(5); 

The above code will create a vector with a capacity for 5 elements. An alternative way is to initialize the vector with some data like so: 

vector<int> myVector = {1,3,7}; 

This will initialize your myVector vector with the values 1, 3 and 7. 

Once you have declared and initialized your vectors, you can start manipulating their elements. To add new elements to the end of your vector, you can use the push_back method: 

myVector.push_back(30); 

This will add 30 as a new element at the end of your myVector vector. If you want to remove elements from your vectors, you can use the pop_back() method which removes the last element from your vector: 

myVector.pop_back(); 

Push_back and Inserting Data In Vectors

When using a vector to store data in C++, it's important to note that the elements being stored must be contiguous in memory. That means that each element must be immediately next to one other in sequence. This makes it easy for the vector container to access individual elements or groups of them quickly and efficiently. 

Inserting data into a vector can be done using either push_back or insert commands. The push_back command adds an item to the end of the existing container while insert allows you to place an item at any point within the container (we'll talk more about random access below). When an item is pushed back or inserted into a vector its size automatically increases by one element which requires additional memory allocation for each new element added on top of the existing elements in the sequence. 

Accessing Elements within a Vector

Accessing elements within a vector can be done using iterator methods, such as begin() and end(). These allow you to iterate through the collection. For example, you could use:


```cpp

// Create vector container 

std::vector<int> vec {10, 20, 30, 40}; 

// Iterator declarations 

std::vector<int>::iterator itr1 = vec.begin(); 

std::vector<int>::iterator itr2 = vec.end(); 

 // Iterate by incrementing the iterator 

 while (itr1 != itr2) { 

     std::cout << *itr1 << ''; 

      ++itr1;   } 

``` 

Analytics Jobs

 You can also access individual elements within a vector using either the at() operator or […] operator with an index notation. For example:

 ```cpp std::cout << vec[2] << ''; // Prints 3rd element in vec  30 ```


Report Page