×

Vector in C++

Vector in C++

In today’s article, we will be learning all the things about vector in C++ and how vector is different form an array in C++.

So basically, vector is very similar to an array in nature but the difference in vector over array is that the size of the array is dynamic and if we talk about an array then the size of an array size is static. Here is an example of an array when we creating an array then we need to put the size inside the square boxes and that would be like:-

int items[19]

Or we can assign something that would help in determining the size and that would be a sequence of values and example for this would be like:-

int items[ ]= {9,10}

but in vector it does not work like this, we can assign vector like this

std:vector<int> item;

We can also assign a sequence of values in vector also like we do in array but it would be like

std:vector<int>item={9,10}

Vector definition

In C++, the most common purpose of the containers is Vector. The vector in C++ supports the dynamic array. That means is an array which can grow when needed. As we know that in C++ the size of the array is fixed at the compiler time.

Although this the best and efficient way for implementing the array, but on the other side of this it also restrictive because we are not allowed to adjust the size of the array at the run time if we need to change some programming conditions. But here comes the role of vector in C++, vector solved this problem by allocating the memory as needed. Vectors in C++are also referred as sequence containers which utilize the continuous storage location to store the elements. But yes as result to this vector also consume more memory to handle the storage in compare to array.

Vector is dynamic in nature but we can use the standard array subscript notations to access the element of the vector.

So the template specification for the vector is given here and that would be like:-

Template<class T, class Allocator = allocator<T>> class vector.

T , here is the type of data that how it’s being stored and allocator specifies the allocator.

How is vector different from array in C++?

Difference between vector and array is as follow:-

VectorArray
Vector occupies more memory than an array occupies.Basically arrays are much more memory efficient.
To create a vector, we need sequential containers to store the elements.To create the array, we only need original data structure, based on the index concept.
In vector, the structs and classes are the same in C++.In array, it does not support class-based declaration but instead of that, it has the interface to support the objects.
The length of the vector varies.The length of the array is fixed size.
The structure of vector is template class and C++ construct.The structure of the array is contiguous memory location.
Talking about resizing in vector it is dynamic in nature.Talking about array it is static and also named as strong typing discipline in terms of resizing.
In vector the access element is time consuming although it is based on position of element.In array the access element is constant and time operation irrespective of the elements location.
Vector is based on non- index structure.Array is basically based on index based with the lowest address as first, and the highest address as the last.
As we know vectors are the sequential containers in C++.Other side array is the lower level data structure in C++.
Vector is basically come from the template class of C++ with the parent as the collection class.On the other hand,arrays are the lowerlevel data structure with their own specific properties.
  • Vector is best for frequent insertion and deletion of element because it automatically increases the size when element is inserted and automatically decreases the size when element is deleted.

Here are some comparisons operators are defined for vector and those are as follow:-

==, <, <=, !=, >, >=.

Subscripting operator which is [] is also defined for vector.  That means it allows us to access the elements of the vector using the standard array subscripting notation.


Related Topics

OOPs Concepts in C++

C++ Object-Oriented Programming Concepts C++ uses the concept of object-oriented programming. Object Oriented Programming has some prominent features: Object Class Data abstraction Encapsulation Polymorphism Inheritance Message passing Object An object is the basic unit...

2 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

6 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

C ++ Program: Alphabet Triangle and Number Triangle

Alphabet Triangle and Number Triangle An alphabet triangle is a triangle that typically looks like a pyramid or other triangles like an isosceles triangle, a right-angled triangle consisting of similar or...

4 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.