C++

Count the size of the vector in C++

The dynamic array can be created by using a vector in C++. One or more elements can be inserted into or removed from the vector at the run time that increases or decreases the size of the vector. The size or length of the vector can be counted using any loop or the built-in function named size(). These ways of counting the size of the vector have been explained in this tutorial by using different examples.

Pre-requisite:

Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code.

Example-1: Count the size of the vector using a loop

Create a C++ file with the following code to count the size of a vector without using any built-in function. A vector of string values has been declared in the code. A user-defined function named calculate_size() has been declared here to calculate the size of the vector using a loop. This function takes the vector as an argument value and returns the size of the vector to the caller. This function has called for the first time after declaring the vector. Next, two values have been added at the end of the vector that will increase the size of the vector. The calculate_size() function has called for a second time to count the size of the modified vector.

#include <iostream>
#include <vector>
using namespace std;

//Declare function to calculate the size of the vector
int calculate_size(vector<string> strVec)
{
    //Initialize a string variable
    int length = 0;
    /*
    Iterate the content of the loop
    and increment the value of the length variable in each iteration
    to count the size of the vector
    */

    for (string element: strVec)
        length++;
    //Return the size value
    return length;
}

int main() {

    //Declare a vector of string data
    vector<string> items = { "Book", "Pen", "Pencil", "Eraser" };
    //Print the current size of the vector
    cout<<"The size of the vector is : "<<calculate_size(items) <<endl;

    //Add two new items using push_back() function
    items.push_back("Color Paper");
    items.push_back("Water color");

    //Print the current size of the vector after addition
    cout<<"The size of the vector after addition is : "<<calculate_size(items) <<endl;
}

Output:

The following output will appear after executing the above code. There were 4 elements in the vector at the time of declaration. So, the output shows that the size of the vector is 4 before inserting the new values, and the size is 6 after inserting 2 values.

Example-2: Count the size of the vector using size()

The built-in function exists in C++ to count the size of the vector. The function name is, size(). It returns the size or the total elements of the vector in which vector it is used. It does not take any argument.

Syntax:

int vector.size();

The following example shows the use of the size() function to count the total elements of a vector. Create a C++ file with the following code to test the code. A vector of integer numbers has been declared in the code. The vector contains 8 elements at the time of declaration. The size() function has been used the first time to count the total elements of the vector and print the count value. The size() function has been used a second time to count the total elements after adding four elements at the end of the vector.

#include <iostream>
#include <vector>

using namespace std;
int main()
{
        //Declare a vector of integer data
        vector<int> intVector{ 56, 89, 45, 87, 21, 77, 10, 42 };

        //Print the size of the vector
        cout << "The size of the vector is : " << intVector.size() << endl;

        //Add some values to the vector using push_back() function
        intVector.push_back(65);
        intVector.push_back(90);
        intVector.push_back(49);
        intVector.push_back(16);

        //Print the size of the vector after addition
        cout << "The size of the vector after addition is : " << intVector.size() << endl;
        return 0;
}

Output:

The following output will appear after executing the above code. There were 8 elements in the vector at the time of declaration. So, the output shows that the size of the vector is 8 before inserting the new values, and the size is 12 after inserting 4 values.

Example-3: Count the size of the vector to insert even numbers

The following example shows how to insert 10 even numbers into the vector after defining the size of the vector. Create a C++ file with the following code to test the code. A vector of integer type with 10 has been declared at the beginning of the code. An integer variable has been declared to insert 10 even numbers from 0 to 18 into the vector. Here, the ‘for’ loop has been used to iterate the vector based on the returned value of the size() function and insert the element into the vector. Next, the output of the size() function has been used to print the vector’s values.

#include <vector>
#include <iostream>
using namespace std;
int main()
{

        //Declare a vector of 10 elements
        std::vector<int> myArray(10);
        //Initialize an integer variable
        int value = 0;

        //Insert even numbers into the vector using size()
        for(int i = 0; i < myArray.size(); i++)
        {
                myArray[i] = value;
                value = value + 2;
        }

        //Print the values of the vector using size()
        for(int j = 0; j < myArray.size(); j++)
                cout << myArray[j] << " ";

        //Add newline
        cout << endl;

}

Output:

The following output will appear after executing the above code.

Conclusion:

Two different ways to count the total elements of a vector have been described in this tutorial using the vector of string data and numeric data. The C++ user will be able to count the size of the vector using a built-in function or loop to solve different programming purposes after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.