Introduction

A dynamic array with auto-scaling capabilities is known as a C++ vector. An element is added to or removed from the vector before the resizing takes place. The container manages the storage automatically. A vector's elements are kept together in contiguous storage. This makes it possible for C++ programmers to use iterators to access and navigate the vector items. A vector's end is where additional information is added. This requires further time. It always takes time to remove an element from a vector. The vector doesn't need to be resized, which is the cause. A vector's initial elements can be added or removed in a linear amount of time.

When to Use a Vector?

  • You need a data structure where parts of data constantly change.
  • If the amount of the data is unknown before commencing, the vector will not need you to select the container's maximum size.

How to Initialize Vectors in C++

The syntax of vectors in C++ is:

vector <data-type> name (items)

  • As was already said, we start with the vector keyword.
  • The vector's elements' data types are indicated by the data-type.
  • The vector's or the data elements' name is the name.
  • The items indicate how many elements make up the data in the vector. This element is not required.

Iterators

Iterators are designed to make it easier for us to access the items that are kept in a vector. It is a thing that functions as a pointer. The typical iterators that C++ vectors support are listed below:

  • vector:: begin():It provides an iterator that leads to the vector's leading member.
  • vector:: end(): It provides an iterator pointing to the vector's past-the-end element.
  • vector::cbegin(): It is identical to vector::begin() but lacks the ability to change the elements.
  • vector::cend(): Similar to vector::end(), but unable to change vector components

Modifiers

Modifiers are used to alter the definition of a data type. The typical C++ modifications are listed below:

  • vector::push_back(): The components are pushed from behind by this modification.
  • vector::insert(): for adding additional objects at a specific place to a vector.
  • vector::pop_back(): The vector elements on the back are removed by this change.
  • vector::erase(): It is used to remove a variety of objects from the designated area.
  • vector::clear(): All of the vector elements are eliminated.

Examples

Example 1

#include <iostream>
#include <vector>

using namespace std;
int main()
{
vector<int> nums;

for (int a = 1; a <= 5; a++)

nums.push_back(a);

cout << "Output from begin and end: ";

for (auto a = nums.begin(); a != nums.end(); ++a)

cout << *a << " ";

cout << "\nOutput from cbegin and cend: ";

for (auto a = nums.cbegin(); a != nums.cend(); ++a)

cout << *a << " ";

return 0;
}

Output

Output from begin and end: 1 2 3 4 5
Output from cbegin and cend: 1 2 3 4 5 

Example 2

#include <iostream>
#include <vector>

using namespace std;
int main()
{
vector<int> nums;

nums.assign(5, 1);

cout << "Vector contents: ";
for (int a = 0; a < nums.size(); a++)
cout << nums[a] << " ";

nums.push_back(2);
int n = nums.size();
cout << "\nLast element: " << nums[n - 1];

nums.pop_back();

cout << "\nVector contents: ";
for (int a = 0; a < nums.size(); a++)
cout << nums[a] << " ";

nums.insert(nums.begin(), 7);

cout << "\nFirst element: " << nums[0];

nums.clear();
cout << "\nSize after clear(): " << nums.size();
}

Output

Vector contents: 1 1 1 1 1
Last element: 2
Vector contents: 1 1 1 1 1
First element: 7
Size after clear(): 0

write your code here: Coding Playground

Explanation

Programs must include the iostream header file to access its features. To access its classes without invoking it, include the std namespace. Create the nums vector to hold some integer values. For each iteration, print the values of the vector nums to the console.

The vector nums is completed by appending the number 2. On the terminal, print some text. The n key shifts the pointer to a new line so that the text may be printed there. To iterate through the members of the vector, use the iterator variable a. Print the first value of vector nums alongside other text.

It should return 7 and print the size of the vector num after clearing all contents. Insert the value 7 to the beginning of the length of the text and print it as an extension of the main() function.

Capacity

Use the following functions to determine the capacity of a vector:

  • Size() – It provides the vector's item count.
  • Max_size() - It gives back as many things as a vector can hold.
  • Capacity () – It gives back how much storage a vector has been given.
  • Resize () – The container is resized to hold n objects. The back elements will be eliminated from the vector if its current size exceeds n. Extra elements will be added to the vector's back if its current size is less than n.
  • Empty () – If a vector is empty, it returns true. It returns false if not.

Summary

A C++ vector is a dynamic array capable of automatically resizing itself when an element is added or deleted from it. The elements of a vector are stored in contiguous storage then traversed using iterators. Vectors should be used when dealing with data elements that change consistently and if the size of the data is not known before beginning.