Нужно сделать сортировку, увеличить массив на 1, добавить значение в конец (PushBack), удалить элемент с конца (PopBack) и реверс массива...
Помогите хотя бы с PushBack, остальное как-то сам домучаю?
#include<ctime>
using namespace std;
class MyArray {
int* data; // ядро класса, указатель на дин. массив
int size; // размер массива, вручную менять нельзя.
public:
MyArray() :MyArray(0)
{ }
MyArray(int size)
{
this->size = size;
this->data = new int[size];
}
MyArray(const MyArray& other) : MyArray(other.size)
{
/*
this->size = other.size;
this->data = new int[size];
*/
for (int i = 0; i < size; i++) {
this->data[i] = other.data[i];
}
}
~MyArray()
{
delete[] data;
}
int GetSize() {
return this->size;
}
void Print() {
cout << "Array " << this <<
". Size: " << size << endl;
cout << "[ ";
for (int i = 0; i < size; i++) {
cout << data[i] << " ";
}
cout << "]" << endl << endl;
}
void SetByIndex(int index, int value) {
if (index < 0 || index >= size) {
cout << "ERROR! Out of array size (SetByIndex)" << endl;
return;
}
else {
data[index] = value;
}
}
int GetByIndex(int index) {
if (index < 0 || index >= size) {
cout << "ERROR! Out of array size (GetByIndex)" << endl;
return 0; // в случае вылезания за массив, возвращаем 0.
}
else {
return data[index];
}
}
void Sort() {
// сортирует массив
}
void FillRand() {
cout << "[ ";
for (int i = 0; i < size; i++) {
data[i] = rand() % 50;
cout << data[i]<<" ";
}
cout << " ]";
}
void pushBack(int value ) {
size++;
cout << "( ";
for (int i = 0; i < size; i++) {
temp[i] = data[i];
}
cout << " )";
int* temp = new int[size];
cout << "[ ";
for (int i = 0; i < size; i++) {
temp[i] = data[i];
cout << temp[i]<<" ";
}
temp[size - 1] = value;
cout << " ]";
delete[] data;
data = temp;
temp = 0;
}
};
void main() {
srand(time(NULL));
MyArray arr(4);
arr.pushBack(5);
arr.FillRand();
cout << endl;
arr.SetByIndex(0, 228);
arr.SetByIndex(1, 1337);
arr.SetByIndex(2, 555);
arr.SetByIndex(3, 785);
for (int i = 0; i < 5; i++) {
cout << arr.GetByIndex(i) << endl;
}
arr.Print();
}```