Top.Mail.Ru
Ответы

Помогите с массивом

Надо удалить все элементы стоящие до максимального в динамическом массиве.

По дате
По рейтингу
Аватар пользователя
Новичок

#include <iostream>
using namespace std;
int main() {
cout << "Length: ";
size_t length;
cin >> length;
auto box = new int[length];
cout << ">>> ";
for (auto i = 0U; i < length; ++i) {
cin >> box[i];
}
auto max = box[0];
auto pos = 0U;
for (auto i = 1U; i < length; ++i) {
if (box[i] > max) {
max = box[i];
pos = i;
}
}
if (pos) {
auto tmp = new int [length - pos];
for (auto i = pos, j = 0U; i < length; ++i, ++j) {
tmp[j] = box[i];
}
delete[] box;
length -= pos;
box = new int[length];
box = move(tmp);
tmp = nullptr;
}
cout << "<<< ";
for (auto i = 0U; i < length; ++i) {
cout << box[i] << ' ';
}
delete[] box;
system("pause > nul");
}

Аватар пользователя
Просветленный