Updated April 18, 2023
Introduction to C++ vector size
In C++, Vectors are called dynamic arrays that can automatically resize themselves when an item is inserted or removed, with its storage being controlled automatically by the container. Vector items are kept in adjacent storage, which is easy to access and traverse with the help of iterators. Moreover, items are inserted at the end, and it may take some time as an extension of the array is needed in certain cases. There are several functions that support vector operations, and size() is one such function that helps in returning the vector size of the container or count of items available in it. In this topic, we are going to learn about the C++ vector size.
Syntax
While learning a new concept in a programming language, you have to understand the same basic syntax. So, let us see the syntax of the size() function in vector.
vec.size()
Here, vec is the name of the vector
Parameters of the function:
Unlike other functions, no parameters are passed in this.
Return value:
Count of items in the container.
Exceptions and Errors
- Do not guarantee exception throw.
- When parameters are passed, errors are thrown.
How to find the size of vector in C++?
As I have already mentioned, size() is one of the vector functions that help in returning the vector size of the container or count of items available in it.
For example, consider a vector vtr as mentioned below.
vector<int> vtr{31, 52, 63, 84, 57 };
From this, we can see that there are 5 elements in the vector. So, when we call the size() function, the result will display the size of the vector as 5.
It can be used while performing addition operations in the vector. Instead of mentioning the size, the size() function can be used.
Examples of C++ vector size
Now, let us see some sample programs on the size function of vector in C++ for a better understanding.
Example #1
CPP program that demonstrates the implementation of size() function in vector
Code:
// C++ Program to demonstrate the implementation of the function size()
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a vector
vector<int> vtr{ 31, 52, 63, 84, 57 };
//print the vector size
cout <<"The vector size is: " << vtr.size();
return 0;
}
Output:
First, this program imports all the necessary header files such as <iostream> and <vector>. After this, a vector is declared with few elements. On executing the code, the size of the vector is printed using the size() function.
Example #2
CPP program that uses size() function in vector for adding all the vector elements.
Code:
// C++ Program that uses function size() for addition of vector elements
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a variable to store the sum
int s = 0;
//declare a vector
vector<int> vtr{ 31, 52, 63, 84, 57 };
//print the vector size
cout <<"The vector size is: " << vtr.size();
while (vtr.size() > 0) {
s = s + vtr.back();
vtr.pop_back();
}
cout <<"\nSum of the vector elements is: " << s;
return 0;
}
Output:
In this program, a vector is declared with few elements and also, a variable s is declared for storing the sum of the elements. On executing the code, the size of the vector is printed using the size() function, and the sum of the elements is printed using a for a loop.
Example #3
CPP program that uses size() function for a string vector
Code:
// C++ Program to demonstrate the implementation of the function size() in string vector
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a vector
vector<string> vtr{ " Do not give up " , " Your miracle " , " on the ", " way "} ;
//variable to store the vector size
int s = vtr.size() ;
//print the vector size
cout <<"The vector size is: " << s ;
return 0;
}
Output:
In this program, a vector is declared with string elements, unlike the above programs. But, the size() function prints the size of the vector.
Example #4
CPP program that creates an empty vector and prints the size
Code:
// C++ Program to create empty vector and print size
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a vector
std::vector<int> vtr;
//variable to store the vector size
int s = vtr.size() ;
//print the vector size
cout <<"The vector size is: " << s ;
return 0;
}
Output:
In this program, a vector is declared with no elements, and on executing the code, the size of the vector will be displayed as 0.
Example #5
CPP program that declares a vector add elements and print size
Code:
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a vector
std::vector<int> vtr;
//add the elements to the vector
for (int i=0 ; i<5 ; i++)
{
vtr.push_back(i);
}
//variable to store the vector size
int s = vtr.size() ;
//print the vector size
cout <<"The vector size is: " << s ;
return 0;
}
Output:
An empty vector is declared in this program, and elements are added to it using a for a loop. Then, the size of the vector is printed using the size() function.
Example #6
CPP program that inserts elements to vector end and print size
Code:
#include <iostream>
#include <vector>
using namespace std;
//main method
int main()
{
//declare a vector
std::vector<int> vtr;
//add the elements to the vector
vtr.insert ( vtr.end() , 5, 50 );
//variable to store the vector size
int s = vtr.size() ;
//print the vector size
cout <<"The vector size is: " << s ;
return 0;
}
Output:
In this program also, an empty vector is declared first, and elements are added to the end of the vector using insert(). Then, the size of the vector is printed using the size() function.
Conclusion
size() is a method that helps in returning the vector size of the container or count of items available in a vector. In this article, different aspects such as syntax, working, and examples of size() function are explained in detail.
Recommended Articles
This is a guide to the C++ vector size. Here we discuss How to find the size of vector work in C++ and Examples along with the codes and outputs. You can also look at the following article to learn more –