Dynamic Array Allocation
You can use dynamic allocation to make arrays of a variable size. This may be more efficient than a vector, but cannot change size.
/* * Dynamic allocation can be used to create variable-sized arrays. */ #include <iostream> #include <memory> #include <utility> using namespace std; int main() { // Ask the size. int size; cout << "How big would you like that array, sir? "; cin >> size; // Allocate a native array with new. It returns a pointer to // the content type, which points to the first item in the // array. int *arr1 = new int[size]; // Read it in. for(int i = 0; i < size; ++i) cin >> arr1[i]; // Or you can use a smart pointer. unique_ptr<int[]> arr2 = std::make_unique<int[]>(size); // Copy the data for(int i = 0; i < size; ++i) { arr2[size-i-1] = arr1[i]; } // The array we got with new we have to get rid of. We must use // the brackets to remind the system that it's an array, since // it's only a pointer to integer. delete [] arr1; // Print the copied array. for(int i = 0; i < size; ++i) { cout << arr2[i] << " "; } cout << endl; // The smart pointer is cleanedup automatically. }