How To Get The Size Of A Vector In C++
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 past the container. Vector items are kept in adjacent storage, which is piece of cake to admission and traverse with the assistance of iterators. Moreover, items are inserted at the end, and information technology may take some time every bit 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 acquire about the C++ vector size.
Syntax
While learning a new concept in a programming language, yous have to empathise the same basic syntax. So, allow us come across the syntax of the size() role in vector.
vec.size()
Hither, vec is the proper noun of the vector
Parameters of the office:
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 work in C++?
Equally 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 beneath.
vector<int> vtr{31, 52, 63, 84, 57 };
From this, we tin can meet that there are 5 elements in the vector. Then, when we call the size() function, the effect 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, allow us meet some sample programs on the size function of vector in C++ for a better understanding.
Example #i
CPP program that demonstrates the implementation of size() function in vector
Code:
// C++ Plan to demonstrate the implementation of the role size()
#include <iostream>
#include <vector>
using namespace std;
//principal method
int master()
{
//declare a vector
vector<int> vtr{ 31, 52, 63, 84, 57 };
//impress the vector size
cout <<"The vector size is: " << vtr.size();
return 0;
}
Output:
First, this program imports all the necessary header files such equally <iostream> and <vector>. Later this, a vector is alleged with few elements. On executing the code, the size of the vector is printed using the size() function.
Example #2
CPP programme that uses size() function in vector for adding all the vector elements.
Lawmaking:
// C++ Plan 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 due south = 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) {
south = southward + vtr.back();
vtr.pop_back();
}
cout <<"\nSum of the vector elements is: " << south;
return 0;
}
Output:
In this plan, a vector is alleged with few elements and also, a variable southward is declared for storing the sum of the elements. On executing the lawmaking, the size of the vector is printed using the size() function, and the sum of the elements is printed using a for a loop.
Case #3
CPP program that uses size() function for a string vector
Code:
// C++ Program to demonstrate the implementation of the function size() in cord vector
#include <iostream>
#include <vector>
using namespace std;
//main method
int chief()
{
//declare a vector
vector<string> vtr{ " Practice not give up " , " Your miracle " , " on the ", " manner "} ;
//variable to shop the vector size
int s = vtr.size() ;
//print the vector size
cout <<"The vector size is: " << s ;
return 0;
}
Output:
In this plan, a vector is declared with string elements, unlike the higher up programs. But, the size() function prints the size of the vector.
Example #four
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;
//primary method
int chief()
{
//declare a vector
std::vector<int> vtr;
//variable to store the vector size
int s = vtr.size() ;
//impress the vector size
cout <<"The vector size is: " << southward ;
return 0;
}
Output:
In this plan, a vector is declared with no elements, and on executing the code, the size of the vector volition be displayed every bit 0.
Example #5
CPP program that declares a vector add elements and impress size
Code:
#include <iostream>
#include <vector>
using namespace std;
//main method
int chief()
{
//declare a vector
std::vector<int> vtr;
//add the elements to the vector
for (int i=0 ; i<five ; i++)
{
vtr.push_back(i);
}
//variable to store the vector size
int south = vtr.size() ;
//impress 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. And then, the size of the vector is printed using the size() function.
Example #6
CPP program that inserts elements to vector cease and print size
Code:
#include <iostream>
#include <vector>
using namespace std;
//principal method
int chief()
{
//declare a vector
std::vector<int> vtr;
//add together the elements to the vector
vtr.insert ( vtr.stop() , v, fifty );
//variable to shop the vector size
int s = vtr.size() ;
//impress the vector size
cout <<"The vector size is: " << southward ;
render 0;
}
Output:
In this programme also, an empty vector is declared first, and elements are added to the cease of the vector using insert(). Then, the size of the vector is printed using the size() part.
Determination
size() is a method that helps in returning the vector size of the container or count of items bachelor in a vector. In this commodity, different aspects such every bit syntax, working, and examples of size() function are explained in detail.
Recommended Articles
This is a guide to the C++ vector size. Hither we discuss How to find the size of vector piece of work in C++ and Examples along with the codes and outputs. Yous can also look at the post-obit article to learn more –
- C++ second Vector
- C++ Void Arrow
- C++ Thread Pool
- C++ Mutable
How To Get The Size Of A Vector In C++,
Source: https://www.educba.com/c-plus-plus-vector-size/
Posted by: peetehiecand.blogspot.com
0 Response to "How To Get The Size Of A Vector In C++"
Post a Comment