Answer to Question #265050 in C++ for Anas

Question #265050

take a scenario where we need to design a function that is supposed to process a vector with 10,000,000 elements.



1
Expert's answer
2021-11-12T17:33:16-0500


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



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS