Home » C++ STL

Push and print elements in a float vector in C++ STL

C++ STL Vector: Here, we are implementing a program in which we are declaring a float vector, pushing the elements and printing the elements.
Submitted by IncludeHelp, on June 18, 2019

In this example, we are declaring a float vector and pushing the elements using push_back() function with the 5 float values and then printing the value separated by space.

Code:

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

int main()
{
    // vector  for float elements
    vector<float> v1;

    // adding 5 elements
    v1.push_back(10.20f);
    v1.push_back(89.30f);
    v1.push_back(12.11f);
    v1.push_back(33.33f);
    v1.push_back(22.19f);

    // print all elements
    cout << "vector elements are: ";
    for (int i = 0; i < v1.size(); ++i) {
        cout << v1[i] << ' ';
    }
    cout << endl;

    return 0;
}

Output

vector elements are: 10.2 89.3 12.11 33.33 22.19

Related posts






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.