take a scenario where we need to design a function that is supposed to process a vector with 10,000,000 elements.
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
#include <iostream>
#include <vector>
using namespace std;
void my_function()
{
//Declaring vector 'my_vector' of integer type
vector<int> my_vector;
//Initializing vector ‘my_vector’ with 10,000,000 elements
for (int i = 1; i <= 10000000; i++)
my_vector.push_back(i);
//Printing size of the vector ‘my_vector’
cout << "Size of our vector is : " << my_vector.size();
//Printing the Capacity of the vector ‘my_vector’
cout << "\nCapacity of our vector is : " << my_vector.capacity();
//Printing the maximum size of the vector ‘my_vector’
cout << "\nMaximum Size of our vector is : " << my_vector.max_size();
//printing element at index 1000000 of the vector ‘my_vector’
cout << "\nElement at index 1000000 is: " << my_vector.at(1000000);
//printing the front element of vector ‘my_vector’
cout << "\nElement at the front is: " << my_vector.front();
//printing the back element of vector ‘my_vector’
cout << "\nElement at the back is: " << my_vector.back();
// checks if the vector is empty or not
if (my_vector.empty() == false)
cout << "\nOur Vector is not empty";
else
cout << "\nOur Vector is empty";
}
int main()
{
//call our function here in the main method
my_function();
return 0;
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment