C/C++

Програмирования С ++ Программирование с использованием ассоциативных контейнеров STL

Составить программу на С++
Контейнер map
Тип элементов double
1 найти максимальный элемент контейнера
2 найти элемент с заданным ключем и удалить его из контейнера
3 к каждому элементу добавит среднее ариымитичесокое контейнера
#include <algorithm>
#include <iostream>
#include <map>
#include <random>
#include <iomanip>
#include <string>
#include <numeric>
using namespace std;
class Random {
public:
Random() {
random_device device;
gen.seed(device());
}
int next(int first, int last) {
uniform_int_distribution<int> range(first, last);
return range(gen);
}
private:
mt19937 gen;
};
string keygen(const size_t length) {
string key;
Random rand;
for (auto i = 0U; i < length; ++i) {
key += rand.next('a', 'z');
}
return key;
}
int main() {
map<string, double> box;
Random rand;
const auto lim = 10U;
for (auto n = 0U; n < lim; ++n) {
auto key = keygen(2);
auto value = rand.next(1, 99) + rand.next(0, 99) / 100.0;
box.insert({ key, value });
}
cout << fixed << setprecision(2);
for (const auto& [key, value] : box) {
cout << key << ':' << setw(6) << value << '\n';
}
puts("");
const auto comp = [](pair<string, double> a, pair<string, double> b) {
return a.second < b.second;
};
auto& [key, max] = *max_element(box.begin(), box.end(), comp);
cout << "Max element: { " << key << ": " << max << " }\n\n";
cout << "Press key: ";
string choice;
cin >> choice;
puts("");
if (box.contains(choice)) {
box.erase(choice);
for (const auto& [key, value] : box) {
cout << key << ':' << setw(6) << value << '\n';
}
puts("");
} else {
puts("Key not found!\n");
}
auto acc = [](double s, pair<string, double> x) {
return s += x.second;
};
auto avg = accumulate(box.begin(), box.end(), .0, acc) / box.size();
cout << "Average: " << avg << "\n\n";
auto add = [avg](pair<const string, double>& x) {
x.second += avg;
};
for_each(box.begin(), box.end(), add);
for (const auto& [key, value] : box) {
cout << key << ':' << setw(7) << value << '\n';
}
system("pause > nul");
}
МК
Мурат Кушуков
92 404
Лучший ответ