akela43

есть ли в двумерном массиве совпадающие элементы.

Jan 6th, 2021 (edited)
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. /**
  2. * есть ли в двумерном массиве совпадающие элементы.
  3. **/
  4. #include <iostream>
  5. #include <set>
  6. #include <vector>
  7. #include <random>
  8. #include <iterator>
  9. using namespace std;
  10. template< typename T> bool isMatch(vector<vector<T>> &v, T & res) {
  11.     set<T> m;
  12.     for (const auto &x : v) {
  13.         for (const auto &y: x) {
  14.             if (m.find(y) != m.end()) {
  15.                 res = y;
  16.                 return true;
  17.             }
  18.             else
  19.                 m.insert(y);
  20.         }
  21.     }
  22.     return false;
  23. }
  24.  
  25. vector<vector<int>> createRand2x(uint col, uint row, bool withPrint = false)    {
  26.     uniform_int_distribution<> uid(1, 100);
  27.     mt19937 gen{ random_device()() };
  28.     auto rand = [&] { return uid(gen); };
  29.     vector<vector<int>> v(row, std::vector<int>(col));
  30.    
  31.     for (auto &x : v) {
  32.         std::generate(x.begin(), x.end(), rand);
  33.         // print
  34.         if (withPrint) {
  35.             copy(begin(x), end(x), ostream_iterator<int>(cout, " "));
  36.             cout.put('\n');
  37.         }
  38.     }
  39.     return v;
  40. }
  41. int main()
  42. {
  43.    
  44.     uint col = 4;
  45.     uint row = 4;
  46.     vector<vector<int>> v = createRand2x(4,4, true);
  47.     int res;
  48.     if (isMatch(v, res))
  49.         cout << res;
  50.     else
  51.         cout << "no matches" ;
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment