I'm trying to create a program that creates an array of a particular size and then extends that array by one. It also needs to display the first array before the function call and the new array after the function call.
#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int[]> newCopy(int arry[], int items)
{
unique_ptr<int[]> p(new int[items + 1]);
p[0] = 0;
for (int i = 0; i < items; i++)
{
p[i + 1] = arry[i];
}
return p;
}
void displayArry(int arry[], int items)
{
for (int i = 0; i < items; i++)
{
cout << arry[i] << " ";
}
cout << endl;
}
int main()
{
const int SIZE = 5;
int myNumbers[SIZE] = {18, 27, 3, 14, 95};
displayArry(myNumbers, SIZE);
unique_ptr<int[]> newArry = newCopy(myNumbers, SIZE);
//displayArry(newArry, SIZE + 1);
for (int i = 0; i < SIZE+1; i++)
{
cout << newArry[i] << " ";
}
return 0;
}
I would like the display function to display both the normal integer array and the smart pointer array without having to overload the function.
The display function works mostly as intended for only the standard integer array. The extension function works and the new unique pointer array displays the correct values if I use a for loop inside of the main function, but I can't seem to figure out how to use both in the display function.
Aucun commentaire:
Enregistrer un commentaire