


Push_back(): The function pushes the elements into a vector from the back. Member Functions of Vectors in C++Ī Vector container in STL provides us with various useful functions. Vector is a dynamic array and, doesn’t needs size declaration. In the above example, a blank vector is being created. To create a vector, you need to follow the below given syntax. Inserting and erasing at the beginning or in the middle of the vector is linear in time.

Removing the last element takes only constant time because no resizing occurs. Inserting an element at the end takes differential time, as sometimes there may be a need of extending the vector. In vectors, data is inserted at the end of it. Vector elements are placed in adjacent storage so that they can be accessed and travel across using iterators. It is not the same in an array where only a given number of values can be stored under a single variable name. Unlike arrays, which are used to store sequential data and are static in nature, Vectors provide more flexibility to the program. Vectors can resize itself automatically when an element is inserted or deleted depending on the need of the task to be executed. Vectors in C++ are the dynamic arrays that are used to store data. They use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. Following Pointers will be covered in this article: Vectors in C++ are sequence containers representing arrays that can change in size.
