Gice

Technology and General Blog

The dynamic array can be developed by working with a vector in C++. A person or far more components can be inserted into or eliminated from the vector at the operate time that boosts or decreases the dimension of the vector. The size or duration of the vector can be counted applying any loop or the built-in perform named sizing(). These ways of counting the dimension of the vector have been spelled out in this tutorial by employing distinct examples.

Pre-requisite:

Before checking the illustrations of this tutorial, you have to test the g++ compiler is installed or not in the process. If you are employing Visible Studio Code, then install the required extensions to compile the C++ supply code to generate the executable code. Here, the Visible Studio Code application has been utilised to compile and execute the C++ code.

Instance-1: Rely the size of the vector utilizing a loop

Develop a C++ file with the pursuing code to count the size of a vector without using any crafted-in functionality. A vector of string values has been declared in the code. A consumer-defined operate named estimate_dimensions() has been declared below to estimate the measurement of the vector making use of a loop. This function normally takes the vector as an argument worth and returns the dimension of the vector to the caller. This functionality has identified as for the 1st time soon after declaring the vector. Future, two values have been extra at the finish of the vector that will improve the size of the vector. The determine_size() function has named for a 2nd time to depend the dimensions of the modified vector.

//Involve important modules

#consist of

#incorporate

usingnamespace std

//Declate operate to work out the dimension of the vector
intcalculate_dimensions(vectorstrVec)

    //Initialize a string variable
    int size =
    /*
    Iterate the material of the loop
    and increment the worth of the length variable in each individual iteration
    to depend the size of the vector
    */

    for (string element: strVec)
        length++
    //Return the measurement price
    return length

intmain()

    //Declare a vector of string facts
    vector products = “Guide”, “Pen”, “Pencil”, “Eraser”
    //Print the existing 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:

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 necessary modules

#include

#include

usingnamespace std
intmain()

    //Declare a vector of integer details
    vectorintVector 56, 89, 45, 87, 21, 77, 10, 42

    //Print the sizing 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
    return0

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 necessary modules

#include

#include

working with namespace std

int main()

//Declare a vector of 10 things

std::vector<int> myArray(10)

//Initialize an integer variable

int value =

//Insert even numbers into the vector applying dimension()

for(int i = 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

<strongusingnamespace std
intmain()

    //Declare a vector of 10 elements
    std::vectormyArray(10)
    //Initialize an integer variable
    int value = 0

    //Insert even numbers into the vector using size()
    for(inti = 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.

Leave a Reply

Your email address will not be published. Required fields are marked *