Software Developer
In C++, vectors are known as dynamic arrays that can routinely resize themselves whilst an item is inserted or eliminated, with its garage being managed routinely by way of the container. Vector items are saved in adjacent storage, which is straightforward to get right of entry to and traverse with the assist of iterators. Furthermore, gadgets are inserted at the cease, and it can make the effort as an extension of the array is wanted in sure cases. Numerous capabilities assist vector operations, and size() is one such function that facilitates returning the vector length of the field or relying upon gadgets available in it. In this topic, we're going to find out about the C++ vector length.
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
Unlike other functions, no parameters are passed in this.
Count of items in the container.
Now, let us see some sample programs on the size function of vectors in C++ for a better understanding.
CPP program that demonstrates the implementation of size() function in vector
// 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;
}
CPP program that uses size() function in vector for adding all the vector elements.
// 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;
}
CPP program that uses size() function for a string vector
// 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;
}
CPP program that creates an empty vector and prints the size
// 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;
}
CPP program that declares a vector add elements and print size
#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;
}
CPP program that inserts elements to vector end and print size
#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;
}