80 lines
2.3 KiB
C++
80 lines
2.3 KiB
C++
/*
|
||
* exercise_1.cpp
|
||
*
|
||
* Created on: 2 окт. 2021 г.
|
||
* Author: alexander
|
||
*/
|
||
|
||
#include <iostream>
|
||
#include <vector>
|
||
#include <random>
|
||
#include <algorithm>
|
||
#include <iterator>
|
||
#include <list>
|
||
|
||
#include "exercise_1.hpp"
|
||
|
||
using namespace std;
|
||
|
||
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
|
||
void generatorInt(TContainer<TItem, TIterator> &c, const unsigned int &i)
|
||
{
|
||
random_device rd;
|
||
mt19937 mersenne(rd());
|
||
uniform_int_distribution<TItem> uid(-static_cast<TItem>(i), i);
|
||
generate(c.begin(), c.end(), [&]()
|
||
{
|
||
return uid(mersenne);
|
||
});
|
||
}
|
||
|
||
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
|
||
void generatorFloat(TContainer<TItem, TIterator> &c, const unsigned int &f)
|
||
{
|
||
random_device rd;
|
||
mt19937 mersenne(rd());
|
||
uniform_real_distribution<TItem> urd(-static_cast<TItem>(f), static_cast<TItem>(f));
|
||
generate(c.begin(), c.end(), [&]()
|
||
{
|
||
return urd(mersenne);
|
||
});
|
||
}
|
||
|
||
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
|
||
void print(const TContainer<TItem, TIterator> &c)
|
||
{
|
||
copy(c.begin(), c.end(), ostream_iterator<TItem> { cout, " " });
|
||
cout << endl;
|
||
}
|
||
|
||
template<template<typename, typename> class TContainer, typename TItem, typename TIterator>
|
||
void insert_sorted(TContainer<TItem, TIterator> &c, const TItem &i)
|
||
{
|
||
typename TContainer<TItem, TIterator>::iterator it = find_if(c.begin(), c.end(), [&](const TItem ¤t)
|
||
{
|
||
return current >= i;
|
||
});
|
||
c.insert(it, i);
|
||
}
|
||
|
||
void exercise_1()
|
||
{
|
||
const unsigned int size = 10;
|
||
int newIntValue = -3;
|
||
double newDoubleValue = 2.2;
|
||
|
||
vector<int> v(size);
|
||
generatorInt(v, size);
|
||
sort(v.begin(), v.end());
|
||
print(v); // -9 -9 -4 3 3 4 6 8 9 10
|
||
insert_sorted(v, newIntValue);
|
||
print(v); // -9 -9 -4 -3 3 3 4 6 8 9 10
|
||
|
||
list<double> l(size);
|
||
generatorFloat(l, size);
|
||
l.sort();
|
||
print(l); // -9.31992 -6.76971 -5.98545 -2.1297 -1.9722 -0.292701 0.715274 3.43089 4.35249 9.45377
|
||
insert_sorted(l, newDoubleValue);
|
||
print(l); // -9.31992 -6.76971 -5.98545 -2.1297 -1.9722 -0.292701 0.715274 2.2 3.43089 4.35249 9.45377
|
||
}
|