Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * есть ли в двумерном массиве совпадающие элементы.
- **/
- #include <iostream>
- #include <set>
- #include <vector>
- #include <random>
- #include <iterator>
- using namespace std;
- template< typename T> bool isMatch(vector<vector<T>> &v, T & res) {
- set<T> m;
- for (const auto &x : v) {
- for (const auto &y: x) {
- if (m.find(y) != m.end()) {
- res = y;
- return true;
- }
- else
- m.insert(y);
- }
- }
- return false;
- }
- vector<vector<int>> createRand2x(uint col, uint row, bool withPrint = false) {
- uniform_int_distribution<> uid(1, 100);
- mt19937 gen{ random_device()() };
- auto rand = [&] { return uid(gen); };
- vector<vector<int>> v(row, std::vector<int>(col));
- for (auto &x : v) {
- std::generate(x.begin(), x.end(), rand);
- // print
- if (withPrint) {
- copy(begin(x), end(x), ostream_iterator<int>(cout, " "));
- cout.put('\n');
- }
- }
- return v;
- }
- int main()
- {
- uint col = 4;
- uint row = 4;
- vector<vector<int>> v = createRand2x(4,4, true);
- int res;
- if (isMatch(v, res))
- cout << res;
- else
- cout << "no matches" ;
- return 0;
- }
Add Comment
Please, Sign In to add comment