Pushback and print elements in a float vector in C++

A vector is an STL container and I’m sure many of you are already familiar with the usability of the vector with any datatype. In this article, we will look at the different ways to pushback and print elements in a float vector.

Adding float elements to vector(pushback)

The float is a datatype to store smaller size decimal numbers in C++. If you want to specify a float constant then you must mention it as the float. Because the C++ compiler considers the default value of decimal to be double(which is a larger datatype and can store big numbers).

To mention a decimal constant as a float you must include an ‘f’ after the number.

We use the  push_back() function of the vector is used to add elements to the end of the vector. In a float vector, the push_back() function can convert a double value to float through implicit conversion. However, adding the ‘f’ can help you and others who read the code in the identification of datatype.

The following example shows how to declare and add elements to a float vector.

#include <iostream>
#include <vector>
int main() {
std::vector<float> fvector; //float vector
fector.push_back(2.4f); // adding f at the end of constant
fvector.push_back(3.14f);
float w=1.8;
fvector.push_back(w);
    return 0;
}

Printing the elements

Typically, we use looping statements to print the elements of a vector. The float vector is no different and we follow the same principle. The for loop is most commonly used for printing elements and we will be looking at using the standard for loop and the range-based for loop.

We know that the vector container has a size() function that returns the number of elements it contains. We can use this size() function for the exit case of the standard for loop.

Before we look at printing elements in a loop, I would like to emphasize a little more on stream manipulators.

Sometimes the floating point number in the vector may be defined until many points after the decimal point which possesses unnecessary precision. To, fix this we use the std::precision() function with the standard std::cout which we will use to print floating-point numbers.

The following example demonstrates the methods we discussed above:

// Online C++ compiler to run C++ program online
#include <iostream>
#include <vector>
#include <iomanip>
int main() {
std::vector<float> fvector; //float vector
fvector.push_back(2.4f); // adding f at the end of constant
fvector.push_back(3.14f);
float w=1.5678937849;
fvector.push_back(w);
std::cout<<"printing using standard for loop:";
for(int i=0;i<fvector.size();i++)
{
    //printing only to 4 significant digits 
    std::cout<<std::setprecision(4)<<fvector[i]<<" "; 
}
std::cout<<std::endl;
std::cout<<"printing using range based for loop:";
for(float f:fvector)
{
    std::cout<<std::setprecision(4)<<f<<" ";
}
    return 0;
}

Output:

printing using standard for loop:2.4 3.14 1.568 
printing using range based for loop:2.4 3.14 1.568

Here although the last number was defined till 10 decimal points only four significant digits were printed because of the std::setprecision() function. As you can see that the range-based for loop is much easier to code and easily readable. Thus, to pushback and print elements in a float vector, we normally use the range-based loop for printing the elements.

Leave a Reply

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