Which of the following statements state the advantage of a vector over an array?

STL stands for Standard Template Library. STL is a set of general-purpose classes and functions which are mainly used for storing and processing data. STL can be defined as a library of container classes, algorithms, and iterators and vectors in C++ is a part of STL. The main idea behind STL is to reuse codes already written and tested. It saves time and effort.

STL has four components

  • Algorithms: It defines a collection of functions specially designed to be used on ranges of elements. Examples are sorting, searching, etc.
  • Containers: Containers store objects and data. There are in total seven standard “first-class” container classes and three container adaptor classes and only seven header files that provide access to these  container adaptors.
  • Functions: STL includes classes which overload the function call operator. Instances of such classes are called functors.
  • Iterators: It is used for working upon a sequence of values.It provides generiality in STL.

What are Vectors in C++?

Vectors are part of STL. Vectors in C++ are sequence containers representing arrays that can change their size during runtime. They use contiguous storage locations for their elements just as efficiently as in arrays, which means that their elements can also be accessed using offsets on regular pointers to its elements.

Vectors are the dynamic arrays that are used to store data.It is different from arrays which store sequential data and are static in nature, Vectors provide more flexibility to the program. Vectors can adjust their size automatically when an element is inserted or deleted from it.

Vectors are not ordered in C++. Vector elements are placed in adjacent storage and  can be easily accessed and traversed across using iterators. In vectors, data is inserted at the end when we use push_back[] function . Inserting an element at the end of a vector takes differential time, as sometimes there may be a need of extending the vector,  but inserting the element at the beginning or at the middle takes linear time. Removing the last element takes only constant time because no resizing takes place. Check out this free online C++ tutorial to learn more and enhance your skills.

Declaration of Vectors in C++

It is mandatory to include #include library before using vectors in C++.

For Vector declaration we need to follow the below syntax:

vector< object_type > vector_variable_name;

Initialization of Vectors

  1. Pushing the values one-by-one in vector using push_back[]:
  • All the elements that need to be stored in the vector are pushed back one-by-one in the vector using the push_back[] method. 
  • Syntax:

vector_name.push_back[element_value];

  1. Using the overload constructor of the vector Class:
  • This method is used to populate a vector with multiple times the same value.
  • Syntax:

vector vector_name [number_of_repetition,element_value];

  1. Using Array:
  • This method uses array as a parameter to be passed in the vector constructor.
  • Syntax:

vector vector_name {val1,val2,val3,....,valn};

  1. Using already initialized vector:
  • This method uses an already created vector to create a new vector with the same values.
  • This method passes the begin[] and end[] of an already initialized vector.

    |
    Syntax:
    vector vector_name_1{val1,val2,…,valn};

vector vector_name_2[vector_name_1.begin[],vector_name_1.end[]]

Various Functions in Vectors are

Iterators:

  • begin[] –  It returns an iterator pointing to the first element in the vector.
  • end[] – It returns an iterator pointing to the last element in the vector.
  • rbegin[] – It returns a reverse iterator pointing to the last element in the vector.
  • rend[] – It returns a reverse iterator pointing to the element preceding the first element in the vector. Basically considered as a reverse end.
  • cbegin[] – It returns a constant iterator pointing to the first element in the vector.
  • cend[] – It returns a constant iterator pointing to the element that follows the last element in the vector.
  • crbegin[] – It returns a constant reverse iterator pointing to the last element in the vector.
  • crend[] – It returns a constant reverse iterator pointing to the element preceding the first element in the vector.

Example Code for Visualizing the use of Iterators:

//C++ Code to Visualize Use of Iterators in C++
#include  
#include  
using namespace std;   
int main[] 
{ 
    vector a; //Declaration of vector in C++
   
   //Initializing vector ‘a’ with values from 1 to 7
    for [int i = 1; i 

Chủ Đề