Write a C++ program to illustrate the concept of standard template library.




Program Algorithm:
Step 1: Include the header files
Step 2: Create a vector to store int
Step 3: Display the original size to store int
Step 4: Push 5 values into the vector using for loop
Step 5: Display extended size of vec
Step 6: Access 5 values from the vector
Step 7: Use iterator to access the values


Program Code in C++:
#include <iostream.h>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
int i;
cout << "vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++)
{
vec.push_back(i);
}
cout << "extended vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++)
{
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end()) 
{
cout << "value of v = " << *v << endl;
v++;
}
return 0;
}





Program Output:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: