Introduction to Vectors in C++
By: Dhruv Trehan
Tags: C Programming, Learning, Programming, Computer Science, Vectors

Welcome Back Readers,

Image Source: Welcome Back

Introduction

I am Dhruv Trehan, Microsoft Student Partner and Coding Blocks Campus Representative from India. This blog is brought to you under the initiative started by me #10DaysofInnovation. I would be sharing by daily learning with the students all around the globe, as a step towards learning in a community. In this blog, I would be documenting the information based on the self-made notes while I was learning Vectors.

Objective

The objective of this article is to acquaint readers with “Basics Vectors in C++” . Relatively, we would be covering all the methods and functions in vectors. We would be designing our own vector class to analyze the implementation of Methods in Vector.

Implementation

Vectors are the containers available in a standard library. They are basically dynamic array (which can grow and shrink in size). Vector types depends upon what the particular vector is going to hold. For example:

vector<char> a vector <int> a

Methods in Vector

In reference to the document shared below, we can overlook:

Three ways of initializing a vector Three ways of Iterating over the values of vector

Basic Methods in Vectors

Functions in a Vector

In reference to the document shared below, we can discuss:

Importance of Push Back

“Adds elements at the end of the memory, expands memory if needed, safe our program from a segmentation fault”

Why Doubling a memory is an expensive operation?

“Whenever memory is doubled using a vector; the previous memory is deleted and new memory is setup, So deletion of a previous memory and setting up a new memory is much more time consuming”

Importance of Push Back function and Doubling a Memory

Confusing operations in Vectors

v.size() returns the current number of elements in a vector

v.capacity() return the total number of elements vector once had, even after deleting some elements

v.max_size() returns the total number of elements vector computer can hold in a worst case

Declaring the size of a vector

Methods in a Vector

d.push_back() is used to add element at the last position of the vector

d.pop_back() is used to delete the last element from the vector

d.insert(d.begin + 3, 100) is used to add element(100) at the 3rd position from beginning

d.insert(d.begin +3,4,100) is used to add element(100) “4 times “ at the 3rd position from beginning

d.erase(d.begin() + 2) is used to remove the element present at the second position in a vector

d.erase(d.begin() + 2,d.begin() + 5) is used to remove multiple elements from the vector at the mentioned positions.

Methods in Vector

d.clear() removes all the element from the vector

d.front() is used to return the first element in a vector

d.back() is used to return the last element in a vector

d. reserve(1000) is used to determine the capacity of the vector or fix the size of vector, in this case it is set to 1000.

Methods in Vector (Contd)

NOTE: “Vectors sometimes does not shrink ; even when the the elements are deleted” “ Reserve method prevents the doubling of the vector and help in faster processing of the vector”

Conclusion

Here we come to end of this blog (Learning of Day 2), hoping to learn something new and share it in a precise way with you all on Day 3. Stay connected.

Image Source: Thank you

Acknowledgement

Special thanks to Coding Blocks C++ Course, for making me understand the concept of vector in such a precise way.