Everything You Need to Know About C++ Vector

Vectors are similar to arrays in some aspects. For example, they are both sequence containers, which means both of these can be accessed sequentially. Although more data structures can be accessed sequentially, in this tutorial, you will learn about C++ vectors in particular.

Below are the topics that will be covered in this tutorial:

  • What is a C++ Vector?
  • Insertion and Deletion of Elements in a Vector
  • Member Functions of a C++ Vector 
  • Modifiers
  • Iterators
  • Capacity
  • Difference between Vector and Array

What is a C++ Vector?

Vectors are sequential containers in which elements are arranged in a serial-wise pattern, or kept in continuous order. They can change their size whenever an element is inserted or removed.

VectorExample_1.

Fig: vector diagram

Syntax:

VectorExample_2

Fig: vector syntax

Here, the object type is the data type of the object like int, string, etc., followed by the variable name.

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Insertion and Deletion of Elements in a Vector

There are some functions in the C++ vector which are used to add and delete a few elements from the vector. You will look into those functions later, but first, you must understand how these elements are inserted inside a vector.

Usually, elements are inserted at the end of the vector. But whenever an element is inserted at a designated position, i.e., place other than the vector end, then it will shift all elements that were after that position to their new position.

In case of deletion, if elements are deleted from the end or the designated position, then it will reduce the size of the container by the number of elements deleted. It will also remove the elements other than the vector end, and will make the elements shift to their new positions.

Member Functions of a C++ Vector

You can categorize a C++ vector member functions into three types :

  • Modifiers
  • Iterators
  • Capacity

Modifiers: As the name suggests, these are functions that are used to modify or change the vector. For example, assign() is used to clear the existing value and assigns a new value to the vector.

Iterators: Iterator functions are used to move across or iterate through the elements of the vector. For example, the end() function is used to point to the last element of the vector.

Capacity: The functions that lie under capacity have something to do with the size like, changing the size of the vector. For example, the resize(n) function is used to change the size of the vector.

Modifiers

  • push_back(): This function allows you to add a new element at the end of the vector.
  • pop_back(): It is used to remove or delete the last element from the vector.
  • insert(): This function is used to add a new element before a specified position inside the vector.
  • erase(): It is used to remove the element from the container at a specified position or a range.
  • swap(): It is used to swap the contents between the vectors, but it should be of the same type.
  • assign(): It is used to assign a new value to the vector by substituting the old value.
  • clear(): This function is used to remove all elements from the vectors.

VectorExample_3

Fig: Modifiers example

In this example, you saw the use of different modifier functions like assign(), push_back(), pop_back(), insert(), clear().

  • Function assign() is assigning 1 to 9 positions in the vector. In this function, the first parameter depicts the number of values, and the second parameter represents the value that is to be assigned.
  • push_back() is adding value 2 at the end of the vector.
  • pop_back() is deleting that last value of the vector.
  • insert() is adding 7 to the starting of the vector because, in the first parameter which specifies the position, you have written n.begin, which means the first element is pointed, and in the second parameter, you have added the value, i.e., 7.
  • At last, the clear() function is removing all the elements from the container. 

Depicted below is the output of the above example.

VectorExample_4.

Fig: Modifiers example output

Iterators

  • begin(): This function returns the iterator to the first element of the vector container.
  • end(): This function returns the iterator to the last element of the vector container.
  • cbegin(): This function is similar to the begin() function; the only difference is that it cannot change or modify the content it is pointing to.
  • cend(): This function also returns the iterator to the last element of the vector, just like the end() function, but the difference is it cannot modify the content it is pointing to.

VectorExample_5

Fig: Iterators example

In this example, you have used one modifier function push_back() and two iterator functions begin() and end(). After the declaration of vector num, you saw the addition of values in it using the push_back() function. Then, you have used a for loop to print those values that you have added, i.e., 1,2,3,4,5. Here num.begin() specifies starting of the loop, and num.end() specifies the ending point of the loop. 

Below is the output of the above example.

VectorExample_6

Fig: Iterators example output

Capacity

  • size(): This function is used to return the number of elements inside the vector.

  • max_size(): It is used to return the maximum size of the vector.

  • resize(n): This function is used to resize the container, i.e., if the given size is greater than n, then the extra elements are removed. And if the size is less than n, then some extra elements are added.

  • capacity(): This function returns the size that is currently allocated to the vector.

  • empty(): It checks whether the vector is empty or not, and it returns true if a vector is empty else it returns false.

VectorExample_7

Fig: Capacity example

In the above example, you saw the use of one modifier function push_back() and some capacity functions like size(), capacity(), max_size(), empty(), resize(). 

You can see in the below output, all of these functions are performing their own specific functionality.

The size() function is returning the size of the container, capacity() is displaying the size that is currently allocated to the container, and so on.

VectorExample_8.

Fig: Capacity example output

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Difference Between Vector and Array

Vector

Array

The size of the vector changes automatically as elements are inserted or removed.

The size of the array is fixed; you cannot change it after initializing its size once.

A vector is less efficient in terms of memory.

An array is more efficient; it occupies less memory than a vector.

Syntax:

             vector <int> v1;

Syntax:

             Int arr[5]={3,2,5,1,6};

It is more suitable when adding and removing operations are done on elements.

It is more suitable for frequent access to elements because of their index-based structure.

Vectors can store a variety of objects.

An array can store similar or homogeneous elements.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

Vectors are sequence containers having the ability to resize themselves. In this tutorial on C++ vectors, you have learned the different member functions of vectors, their functionalities, and the difference between vectors and arrays.

Do you have any questions regarding C++ vectors? If you have, then please put them in the comments section. We’ll help you solve your queries. To learn more about how to initialize a Vectorin C++ and C++ String checkout our C++ tutorial.

If you on the other hand are looking for more comprehensive learning that goes beyond C++ and covers the most in-demand programming languages and skills needed today, Simplilearn’s Full Stack Java Developer Master's Program will prove to be just right for you. This globally-recognized Bootcamp covers 30+ in-demand tools and skills - from SpringBoot to Angular, Hibernate, JSPs, Servlets, and much more. Immerse yourself in this applied learning curriculum where you get to perfect your skills with 20 lesson-end, 6 phase-end projects and a capstone project in 4 domains. Explore now and get started.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.