#include
#include
#include
#include
using namespace std;
void fill_random(int * arr, const int size, const int min, const int max);
void print(const int * arr, const int size);
int * delete_zero(int * arr, int * size);
int * insert(int * arr, int * size, int * to, const int value);
int main()
{
int size = 10;
int * arr = new int[size];
fill_random(arr, size, 0, 5);
print(arr, size);
arr = delete_zero(arr, &size);
int * pos = find_if(arr,arr+size, [](int val){ return !(val%2);} );
if (pos != arr+size)
arr = insert(arr, &size, pos, *(pos)+2);
print(arr, size);
delete [] arr;
return 0;
}
void fill_random(int * arr, const int size, const int min, const int max)
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution dist(min, max);
generate(arr, arr+size, [&dist, &gen]() -> int
{ return dist(gen); });
}
void print(const int * arr, const int size)
{
copy(arr,arr+size, ostream_iterator(cout," "));
cout<=arr && to<(arr+*size) )
{
int * new_arr = new int[*size+1];
int b_size = to-arr+1;
memcpy(new_arr, arr, sizeof(int) * b_size);
*(new_arr + b_size) = value;
memcpy(new_arr+b_size+1, arr+b_size, sizeof(int) * (*size-b_size) );
delete [] arr;
++*size;
return new_arr;
}
return arr;
}
C/C++
ПРОВЕРЬТЕ ПОЖАЛУЙСТА, НЕ ПОНИМАЮ ОШИБКУ
#include <algorithm>
#include <iostream>
#include <random>
using namespace std;
int* create(int size);
int* destroy(int* arr);
void fill_random(int* arr, int size, int min, int max);
void print(int* arr, int size);
int* delete_zero(int* arr, int& size);
int* insert(int* arr, int& size, int* to, int value);
int main() {
int size = 10;
auto arr = create(size);
fill_random(arr, size, 0, 5);
print(arr, size);
arr = delete_zero(arr, size);
print(arr, size);
auto pred = [](int x) { return x & 1; };
auto pos = find_if(arr, arr + size, pred);
if (pos != arr + size) {
arr = insert(arr, size, pos, *(pos)+2);
print(arr, size);
}
arr = destroy(arr);
system("pause > nul");
}
int* create(int size) {
return new int[size];
}
int* destroy(int* arr) {
if (arr != nullptr) {
delete[] arr;
arr = nullptr;
}
return arr;
}
void fill_random(int* arr, int size, int min, int max) {
uniform_int_distribution<> uid(min, max);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
generate(arr, arr + size, rand);
}
void print(int* arr, int size) {
auto print = [](int x) { cout << x << ' '; };
for_each(arr, arr + size, print);
puts("");
}
int* delete_zero(int* arr, int& size) {
auto zero = find(arr, arr + size, 0);
auto is_zero = [](int x) { return x == 0; };
auto it = remove_if(arr, arr + size, is_zero);
auto nsize = it - arr;
auto tmp = create(nsize);
copy(arr, arr + nsize, tmp);
size = nsize;
arr = destroy(arr);
arr = create(size);
copy(tmp, tmp + size, arr);
tmp = destroy(tmp);
return arr;
}
int* insert(int* arr, int& size, int* to, int value) {
auto tmp = create(size + 1);
copy(arr, arr + size, tmp);
auto it = tmp + size;
to = tmp + (to - arr);
while (it != to) {
*it = *(it - 1);
--it;
}
*it = value;
++size;
arr = destroy(arr);
arr = create(size);
copy(tmp, tmp + size, arr);
tmp = destroy(tmp);
return arr;
}
#include <iostream>
#include <random>
using namespace std;
int* create(int size);
int* destroy(int* arr);
void fill_random(int* arr, int size, int min, int max);
void print(int* arr, int size);
int* delete_zero(int* arr, int& size);
int* insert(int* arr, int& size, int* to, int value);
int main() {
int size = 10;
auto arr = create(size);
fill_random(arr, size, 0, 5);
print(arr, size);
arr = delete_zero(arr, size);
print(arr, size);
auto pred = [](int x) { return x & 1; };
auto pos = find_if(arr, arr + size, pred);
if (pos != arr + size) {
arr = insert(arr, size, pos, *(pos)+2);
print(arr, size);
}
arr = destroy(arr);
system("pause > nul");
}
int* create(int size) {
return new int[size];
}
int* destroy(int* arr) {
if (arr != nullptr) {
delete[] arr;
arr = nullptr;
}
return arr;
}
void fill_random(int* arr, int size, int min, int max) {
uniform_int_distribution<> uid(min, max);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
generate(arr, arr + size, rand);
}
void print(int* arr, int size) {
auto print = [](int x) { cout << x << ' '; };
for_each(arr, arr + size, print);
puts("");
}
int* delete_zero(int* arr, int& size) {
auto zero = find(arr, arr + size, 0);
auto is_zero = [](int x) { return x == 0; };
auto it = remove_if(arr, arr + size, is_zero);
auto nsize = it - arr;
auto tmp = create(nsize);
copy(arr, arr + nsize, tmp);
size = nsize;
arr = destroy(arr);
arr = create(size);
copy(tmp, tmp + size, arr);
tmp = destroy(tmp);
return arr;
}
int* insert(int* arr, int& size, int* to, int value) {
auto tmp = create(size + 1);
copy(arr, arr + size, tmp);
auto it = tmp + size;
to = tmp + (to - arr);
while (it != to) {
*it = *(it - 1);
--it;
}
*it = value;
++size;
arr = destroy(arr);
arr = create(size);
copy(tmp, tmp + size, arr);
tmp = destroy(tmp);
return arr;
}
для начала покажи что пишет компилятор.
Похожие вопросы
- Проверьте пожалуйста правильно ли написана программа вопроса
- Помогите пожалуйста не понимаю как делать. Буду очень благодарен
- Помогите, пожалуйста, исправить ошибку в коде (C++).
- Помогите исправить ошибку с С++ с отдельными переменными не понимаю где нужно прописать
- Ошибка C2280 предпринята попытка ссылки на удаленную функцию C++
- Помогите найти ошибки в коде на C++, не выводится последняя строчка
- Как решить ошибку C2079 использует не имеющую определения структуру class C++
- Как решить ошибки E0144 и E0077 C++
- Ошибка в динамическом массиве new[] и delete[] |С++ Builder
- Синтаксическая ошибка класса. C++
49:48: error: 'memcpy' was not declared in this scope
In function 'int* insert(int*, int*, int*, int)':
59:50: error: 'memcpy' was not declared in this scope
2 то же самое.