How to Read Elements From a Vector

The vector is used in C++ to create the dynamic assortment and the size of the vector can be changed by adding or removing the elements. The at() function of the vector is used to access the element of the particular position that exists in the vector. Information technology throws an exception if the position value is invalid. The uses of the at() function in the C++ vector have shown in this tutorial.

Syntax:

The syntax of this function is given below. This function takes the specific position every bit an argument value and returns the value of that position if the position value exists.

vector.at(position)

Case-1: Read each chemical element of a vector

The way to print each chemical element of the vector by using at() function has shown in the following example. A vector of string values has been defined in the lawmaking.

The 'for' loop has been used to iterate the vector based on the vector size and impress each value of the vector in each line by using at() function. The size() function has been used to count the total number of elements of the vector.

//Include library for input and output

#include<iostream>

#include<vector>

using namespace std;

int principal( )
{
//Declare a vector of string values
vector Str = { "Welcome" , "to" , "LinuxHint" } ;
//Iterate the string using loop
for ( int i= 0 ; i<Str.size ( ) ; i++ )
{
//Print the character
cout<< Str.at (i) << "\due north" ;
}
cout<< "\n" ;
return 0 ;
}

Output:

The post-obit output will appear after executing the above code. At that place are 3 elements in the vector that have been printed in the output.

Example-2: Summate the sum of the vector values

The mode 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 offset 'for' loop has been used to impress the values of the vector and the second 'for' loop has been used to calculate the sum of all values of the vector. Side by side, the result of the summation has been printed.

//Include necessary modules

#include <iostream>

#include <vector>

using namespace std;

int main ( )
{
//Declare a vector of integer numbers
vectorintVector { 7 , iv , 9 , 2 , i , 0 , viii , 3 , 5 , 6 } ;
//Initialize an integer variable
int sum = 0 ;
//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) ;

//Impress the sum value
cout<< "The sum of all vector values is:" << sum << "\n" ;

render 0 ;
}

Output:

The following output will appear after executing the to a higher place lawmaking. The sum of all values (7 + 4 + nine + 2 + 1 + 0 + 8 +iii +five + 6) is 45 that has printed in the output.

Instance-3: Search a item value in the vector

The style to search a particular value in a vector using at() part has shown in the post-obit instance. A vector of x integer numbers has been used in the code like the previous case. The values of the vector take been printed past using a 'for' loop and at() part.

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 up the value of the variable, found to be true if the input value exists in the vector.

//Include necessary modules

#include <iostream>

#include <vector>

using namespace std;

int primary ( )
{
//Declare a vector of integer numbers
vectorintVector { vii , 4 , 9 , 2 , 1 , 0 , eight , three , 5 , 6 } ;
//Initialize an integer variable
bool constitute = simulated ;
int number;

//Impress 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' ;

//Accept a number to search
cout<>number;
//Search the number into the vector using loop
for ( int i= 0 ; i<intVector.size ( ) ; i++ )
{
if (intVector.at (i) == number)
{
institute = true ;
interruption ;
}
}
//Print bulletin based on the search result
if (institute == true )
cout<< "The number is found.\north" ;
else
cout<< "The number is not found.\n" ;
render 0 ;
}

Output:

The following output will appear afterward executing the to a higher place lawmaking. The value 2 exists in the vector, and the message, "The number is institute" has been printed.

The value xi does not exist in the vector and the message, "The number is not found" has been printed.

Example-iv: Search value based on atmospheric condition in the vector

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

#include <iostream>

#include <vector>

#include <cmath>

using namespace std;
int main( )
{
//Declare an empty vector
vectorintVector;
//Declare an integer number
int number;
for ( int i= 0 ; i< 5 ; i++ )
{
//Take a number to insert
cout<>number;
//Insert the number
intVector.push_back (number) ;
}

/*
Print the numbers which are divisible
by 5 and iii
*/

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 post-obit output volition appear after executing the above lawmaking for the input values, five, 9, 15, viii, and 45. Hither, xv and 45 are divisible past 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 too be used for string information type. I hope the reader volition be able to apply this function in vector for various purposes after reading this tutorial.

About the author

I am a trainer of spider web programming courses. I like to write article or tutorial on diverse 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 Aid.

hansonwhiment.blogspot.com

Source: https://linuxhint.com/at-functions-cpp-vector/

0 Response to "How to Read Elements From a Vector"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel