Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5.  
  6. /**
  7.  * Genera el producto cartesiano entre todos los items de todos
  8.  * los arrays de vectores.
  9.  */
  10. std::vector<std::vector<int> > cart_product(std::vector<std::vector<int>>& v) {
  11.     std::vector<std::vector<int>> s = {{}};
  12.     for (auto& u : v) {
  13.         std::vector<std::vector<int>> r;
  14.         for (auto& x : s) {
  15.             for (auto y : u) {
  16.                 r.push_back(x);
  17.                 r.back().push_back(y);
  18.             }
  19.         }
  20.         s.swap(r);
  21.     }
  22.     return s;
  23. }
  24.  
  25. int main(int argc, char const *argv[]) {
  26.     std::vector<std::vector<int>> elInput;
  27.     elInput.push_back({-1, 0, 1});
  28.     elInput.push_back({-1, 0, 1});
  29.     elInput.push_back({-1, 0, 1});
  30.  
  31.     std::vector<std::vector<int>> elOutput = cart_product(elInput);
  32.  
  33.     // mostrar el resultado
  34.     for (int i = 0; i < elOutput.size(); ++i) {
  35.         for (int j = 0; j < elOutput[i].size(); ++j) {
  36.             std::cout << elOutput[i][j] << " ";
  37.         }
  38.         std::cout << std::endl;
  39.     }
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement