C++ vector size

 C++ vector size
Techiio-author
Written by Sagar RabidasJanuary 28, 2022
7 min read
C++
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start discussing C++ vector size.

C++ vector size

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.

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.

Examples of C++ vector size

Now, let us see some sample programs on the size function of vectors 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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

C
C++
C++ vector size
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance