Vector Size Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Size Function

Vector Size Function

BLUF: Mastering Vector Size Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Size Function

C++ is renowned for its efficiency. Learn how Vector Size Function enables low-level control and high-performance computing in the tutorial below.

C++ Vector size

It calculates the quantity of items in the vector.

Syntax

Consider a vector 'v' and a count of elements 'n'. The syntax is as follows:

Example

int n=v.size();

Parameter

It does not contain any parameter.

Return value

It returns the number of elements in the vector.

Example 1

Let's see a simple example.

Example

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<string> v{"Welcome to javaCppTutorial","c"};
	int n=v.size();
	cout<<"Size of the string is :"<<n;
	return 0;
}

Output:

Output

Size of the string is:2

In this instance, vector v holds two strings, and the size method returns the count of elements within the vector.

Example 2

Let's see a another simple example.

Example

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int> v{1,2,3,4,5};
	int n=v.size();
	cout<<"Size of the vector is :"<<n;
	return 0;
}

Output:

Output

Size of the vector is :5

In this instance, vector v holds integer values and the size method calculates the total number of elements stored in the vector.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience