Gice

Technology and General Blog

The vector is employed in C++ to produce the dynamic array and the dimensions of the vector can be altered by including or eliminating the things. The at() operate of the vector is utilised to entry the aspect of the particular posture that exists in the vector. It throws an exception if the position price is invalid. The employs of the at() perform in the C++ vector have revealed in this tutorial.

Syntax:

The syntax of this operate is presented down below. This function will take the distinct situation as an argument benefit and returns the value of that placement if the place value exists.

vector.at(situation)

Case in point-1: Examine just about every element of a vector

The way to print every element of the vector by working with at() purpose has demonstrated in the subsequent illustration. A vector of string values has been defined in the code.

The ‘for’ loop has been utilized to iterate the vector primarily based on the vector size and print each individual benefit of the vector in each line by applying at() operate. The size() functionality has been utilised to count the full selection of things of the vector.

//Incorporate library for enter and output

#include

#contain

working with namespace std

int main()

    //Declare a vector of string values
    vector Str = “Welcome”, “to”, “LinuxHint”
    //Iterate the string making use of loop
    for(int i= i<Str.size() i++)
   
        //Print the character
        cout<< Str.at(i) << n
   
    cout<< n
    return 0

Output:

The following output will appear after executing the above code. There are 3 elements in the vector that have been printed in the output.

Example-2: Calculate the sum of the vector values

The way to calculate the sum of all values of a vector that contains integer numbers has shown in the following example. A vector of 10 integer numbers has been declared in the code. The first ‘for’ loop has been used to print the values of the vector and the second ‘for’ loop has been used to calculate the sum of all values of the vector. Next, the result of the summation has been printed.

//Include necessary modules

#include

#involve

working with namespace std

int key ()

    //Declare a vector of integer numbers
    vectorintVector 7, 4, 9, 2, 1, , 8, 3, 5, 6
    //Initialize an integer variable
    int sum =
    //Print the values of the vector
    cout<< “The values of the vector:n
    for (int i=0 i<intVector.size() i++)
        cout<< ‘ ‘ << intVector.at(i)
    cout<< n

    //Calculate the sum of the vector values
    for (int i=0 i<intVector.size() i++)
        sum += intVector.at(i)

    //Print the sum value
    cout<< “The sum of all vector values is:” << sum << n

    return 0

Output:

The following output will appear after executing the above code. The sum of all values (7 + 4 + 9 + 2 + 1 + 0 + 8 +3 +5 + 6) is 45 that has printed in the output.

Example-3: Search a particular value in the vector

The way to search a particular value in a vector using at() function has shown in the following example. A vector of 10 integer numbers has been used in the code like the previous example. The values of the vector have been printed by using a ‘for’ loop and at() function.

The searching value will be taken from the user. Another ‘for’ loop has been used to search the input value into the vector and set the value of the variable, found to be true if the input value exists in the vector.

//Include necessary modules

#include

#include things like

working with namespace std

int key ()

    //Declare a vector of integer numbers
    vectorintVector 7, 4, 9, 2, 1, , 8, 3, 5, 6
    //Initialize an integer variable
    bool discovered = bogus
    int range

    //Print the values of the vector
    cout<< “The values of the vector:n
    for (int i=0 i<intVector.size() i++)
        cout<< ‘ ‘ << intVector.at(i)
    cout<< n

    //Take a number to search
    cout<>selection
    //Search the amount into the vector employing loop
    for (int i= i<intVector.size() i++)
   
        if(intVector.at(i) == number)
       
            found = true
            break
       
   
    //Print message based on the search result
    if(found == true)
        cout<< “The number is found.n
    else
        cout<< “The number is not found.n
    return 0

Output:

The following output will appear after executing the above code. The value 2 exists in the vector, and the message, “The number is found” has been printed.

The value 11 does not exist in the vector and the message, “The number is not found” has been printed.

Example-4: Search value based on conditions in the vector

The way to find out those values from the vector that are divisible by 5 and 3 has shown in the following example. Five integer values will be taken from the user and inserted into an empty vector by using the push_back() function. After insertion, each value of the vector will be divided by 3 and 5. If the remainder values of both divisions are zero, then that value of the vector will be printed.

#include

#include

#incorporate

employing namespace std
int key()

    //Declare an vacant vector
    vectorintVector
    //Declare an integer selection
    int quantity
    for (int i= i<5 i++)
   
        //Take a number to insert
        cout<>selection
        //Insert the quantity
        intVector.push_again(range)
   

    /*
    Print the quantities which are divisible
    by 5 and 3
    */

    cout<< “The numbers divisible by 5 and 3 are:n
    for (int i=0 i<intVector.size() i++)
        if(intVector.at(i) % 5 ==0 && intVector.at(i) %3 == 0)
        cout<< intVector.at(i) << ” “
   
    cout<< n
    return 0

Output:

The following output will appear after executing the above code for the input values, 5, 9, 15, 8, and 45. Here, 15 and 45 are divisible by 3 and 5.

Conclusion:

Different uses of at() function in C++ vector have been described in this tutorial by using multiple examples. The at() function can also be used for string data type. I hope the reader will be able to use this function in vector for various purposes after reading this tutorial.

Leave a Reply

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