Skip to main content

Posts

Showing posts with the label Write a function that creates a vector of user-given size M using new operator in a program

Write a function that creates a vector of user-given size M using new operator in a program

  here's an example program that creates a vector of user-given size M using the new operator: #include <iostream> #include <vector> using namespace std; // Function to create a vector of given size using new operator vector<int>* createVector(int size) {     return new vector<int>(size); } int main() {     int M;     cout << "Enter the size of vector: ";     cin >> M;     // Create vector using new operator     vector<int>* myVector = createVector(M);     // Display vector size     cout << "Vector size: " << myVector->size() << endl;     // Deallocate memory used by vector     delete myVector;     return 0; } In this program, we define a function called createVector that takes an integer argument size and returns a pointer to a new std::vector<int> object created using the new operator with the specified ...