C++
- 9 ответов
- 0 вопросов
7
Вклад в тег
#include <iostream>
#include <vector>
#include <climits>
#include <map>
#include <ctime>
class MyMatrixInt {
public:
MyMatrixInt(size_t rows = 1, size_t cols = 1, int range = 1)
{
reFill(rows, cols, range);
}
void reFill(size_t rows = 1, size_t cols = 1, int range = 1)
{
data_.clear();
for (size_t i = 0; i < rows; ++i) {
std::vector<int> row;
for (size_t j = 0; j < cols; ++j) {
row.push_back(rand() % range);
}
data_.push_back(row);
}
}
int maxUniqueElem() const
{
int max_elem = INT_MIN;
std::map<int, int> elem_map;
for (size_t i = 0; i < data_.size(); i++) {
for (size_t j = 0; j < data_[i].size(); j++) {
elem_map[data_[i][j]]++;
}
}
std::map<int, int>::reverse_iterator rit;
for (rit = elem_map.rbegin(); rit != elem_map.rend(); ++rit) {
if (rit->second == 1) {
max_elem = rit->first;
break;
}
}
return max_elem;
}
void print() const
{
for (size_t i = 0; i < data_.size(); ++i) {
for (size_t j = 0; j < data_[0].size(); j++) {
std::cout << data_[i][j] << "\t";
}
std::cout << std::endl;
}
}
private:
std::vector<std::vector<int> > data_;
};
int main()
{
MyMatrixInt my_mat;
//srand(time(NULL));
my_mat.reFill(5, 5, 10); // Fill 2D 5x5 Matrix
my_mat.print();
int max = my_mat.maxUniqueElem();
if (max != INT_MIN) {
std::cout << "\nMax element is: " << max << std::endl;
}
else {
std::cout << "\nMax element in matrix isn't found!" << std::endl;
}
return 0;
}
while (!(cin >> D[m]))
while (!(cin >> D[i]))
cout << "Введите кол-во массивов: ";
cout << "Введите кол-во элементов массива ";