Advertisement
Guest User

Untitled

a guest
Jun 15th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <algorithm>
  5. #include <chrono>
  6.  
  7. using namespace std;
  8.  
  9. struct Pos{
  10.     size_t line;
  11.     size_t row;
  12. };
  13.  
  14. class Cell {
  15. public:
  16.     Cell(int _val) : val(_val){}
  17.     void setPosition(const Pos& p){
  18.         pos.line = p.line;
  19.         pos.row = p.row;
  20.     }
  21.  
  22.     size_t Row() const {
  23.         return pos.row;
  24.     }
  25.  
  26.     size_t Line() const {
  27.         return pos.line;
  28.     }
  29.  
  30.     int Value() const {
  31.         return val;
  32.     }
  33.  
  34. private:
  35.     int val;
  36.     Pos pos;
  37. };
  38.  
  39. size_t Random(default_random_engine &d){
  40.     uniform_int_distribution<> uid(1, 1000);
  41.     return uid(d);
  42. }
  43.  
  44. int main() {
  45.     const int size = 10;
  46.     vector<vector<Cell*>> matrix(size, vector<Cell*>(size));
  47.     vector<Cell*> rowMax;
  48.  
  49.     default_random_engine dre (chrono::steady_clock::now().time_since_epoch().count());
  50.  
  51.     for(size_t row = 0; row < size; ++row){
  52.         for(size_t line = 0; line < size; ++line){
  53.             auto cell = new Cell(Random(dre));
  54.             cell->setPosition({row, line});
  55.             matrix[row][line] = cell;
  56.         }
  57.     }
  58.  
  59.     auto cmp = [](const auto lhs, const auto rhs){ return lhs->Value() < rhs->Value(); };
  60.  
  61.     for(const auto& line : matrix){
  62.         auto it = max_element(line.begin(), line.end(), cmp);
  63.         rowMax.push_back(*it);
  64.     }
  65.  
  66.     cout << "Line: ";
  67.     auto it = max_element(rowMax.begin(), rowMax.end(), cmp);
  68.     auto maxLine = std::distance(rowMax.begin(), it);
  69.     cout << maxLine  << endl;;
  70.  
  71.     cout << "Row: ";
  72.     for(size_t i = 0; i<size; ++i){
  73.         cout << matrix.at(maxLine).at(i)->Value() << " ";
  74.     }
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement