vector in c++

How to Make C++ Vector using STL Explained 2020

Vector in C++

We Already know how to declare an array in our previous post. In this post, we focus on how to declare an array as c++ vector using c++ STL library. Most importantly we discussed the c++ header library to make an array of dynamic sizes in this post.

However, Some of the most commonly used classes for implementing sequential lists or arrays are

Vector

List

Let’s look at vector in this post in details.

Vector

A vector in C++ STL is a class that represents a dynamic array. The advantages of vector over normal arrays are,

  • We do not need to pass size as an extra parameter when we pass a vector.
  • Vectors have many in-built functions for erasing an element, inserting an element, etc.
  • Vectors support dynamic sizes, we do not have to initially specify the size of a vector. However, resizing of vector also be done.
  • There are many other functionalities vector provide.

Vectors are dynamic Array

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. To clarify, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. Removing the last element takes only constant time because no resizing happens but Inserting and erasing at the beginning or in the middle is linear in time. In short , vectors are purely dynamic array.

To use the Vector class, include the below header file in your program:

#include< vector >


Declaring Vector:

vector< Type_of_element > vector_name;

For example, Type_of_element can be any valid C++ data type,
or can be any other container also like Pair, List etc.

Important Function in Vector

  • begin() – Returns an iterator pointing to the first element in the vector.
  • end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector.
  • size() – Returns the number of elements in the vector.
  • capacity() – Returns the size of the storage space currently allocated to the vector expressed as the number of elements.
  • empty() – Returns whether the container is empty. In other words, it gives you boolean value.
  • push_back() – It pushes the elements into a vector from the back.
  • pop_back() – Poping from the back is done by pop_back function
  • insert() – It inserts new elements before the element at the specified position.
  • erase() – It is used to remove elements from a container from the specified position or range.
  • swap() – It is used to swap the contents of one vector with another vector of the same type and size.
  • clear() – It is used to remove all the elements of the vector container.
  • emplace() – It extends the container by inserting a new element at the position.
  • emplace_back() – It is used to insert a new element into the vector container, but, the new element is added to the end of the vector.



// C++ program to illustrate the above functions

#include <iostream> 
#include <vector> 

using namespace std; 

int main() 
{ 
    vector<int> v; 
    
    // Push elements 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i); 

    cout << "Size : " << v.size(); 
    
    // checks if the vector is empty or not 
    if (v.empty() == false) 
        cout << "\nVector is not empty"; 
    else
        cout << "\nVector is empty"; 

    cout << "\nOutput of begin and end: "; 
    for (auto i = v.begin(); i != v.end(); ++i) 
        cout << *i << " "; 
        
    // inserts at the beginning 
    v.emplace(v.begin(), 5); 
    cout << "\nThe first element is: " << v[0]; 
  
    // Inserts 20 at the end 
    v.emplace_back(20); 
    int n = v.size(); 
    cout << "\nThe last element is: " << v[n - 1]; 
  
    // erases the vector 
    v.clear(); 
    cout << "\nVector size after erase(): " << v.size();     

    return 0; 
}

Output:
Size : 5
Vector is not empty
Output of begin and end: 1 2 3 4 5 
The first element is: 5
The last element is: 20
Vector size after erase(): 0

Example 1 – Try Hand on this Leetcode problem

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

16 Comments

  1. I am now not positive the place you are getting your information, however good topic. I needs to spend a while learning much more or working out more. Thanks for magnificent info I was in search of this information for my mission.

  2. Usually I do not learn post on blogs, but I wish to say that this write-up very pressured me to take a look at and do so! Your writing style has been surprised me. Thanks, very great article.

  3. There may be noticeably a bundle to learn about this. I assume you made certain good factors in options also.

  4. I was curious if you ever thought of changing the layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two images. Maybe you could space it out better?

  5. I just like the valuable information you supply on your articles. I’ll bookmark your weblog and test once more right here frequently. I’m moderately sure I’ll be told lots of new stuff proper right here! Good luck for the following!

  6. I appreciate, cause I found exactly what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye

  7. Nice read, I just passed this onto a colleague who was doing some research on that. And he actually bought me lunch since I found it for him smile Therefore let me rephrase that: Thanks for lunch!

  8. naturally like your website however you have to take a look at the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very bothersome to inform the truth on the other hand I will certainly come again again.

  9. Thanks for your marvelous posting! I quite enjoyed reading it, you might be a great author.I will be sure to bookmark your blog and will come back sometime soon. I want to encourage you continue your great writing, have a nice morning!

  10. My brother suggested I may like this web site. He used to be entirely right. This post actually made my day. You can not consider just how a lot time I had spent for this information! Thank you!

  11. I love what you guys tend to be up too. This sort of clever work and exposure! Keep up the wonderful works guys I’ve you guys to my personal blogroll.

  12. I enjoy, cause I discovered just what I used to be taking a look for. You’ve ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

  13. Good day! Do you know if they make any plugins to assist with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success. If you know of any please share. Appreciate it!

  14. Hello, you used to write magnificent, but the last few posts have been kinda boring… I miss your super writings. Past several posts are just a bit out of track! come on!

  15. Hello there! Do you use Twitter? I’d like to follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward to new updates.

  16. Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and help others like you aided me.

Leave a Comment

Your email address will not be published. Required fields are marked *