Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <utility>
- /**
- * Genera el producto cartesiano entre todos los items de todos
- * los arrays de vectores.
- */
- std::vector<std::vector<int> > cart_product(std::vector<std::vector<int>>& v) {
- std::vector<std::vector<int>> s = {{}};
- for (auto& u : v) {
- std::vector<std::vector<int>> r;
- for (auto& x : s) {
- for (auto y : u) {
- r.push_back(x);
- r.back().push_back(y);
- }
- }
- s.swap(r);
- }
- return s;
- }
- int main(int argc, char const *argv[]) {
- std::vector<std::vector<int>> elInput;
- elInput.push_back({-1, 0, 1});
- elInput.push_back({-1, 0, 1});
- elInput.push_back({-1, 0, 1});
- std::vector<std::vector<int>> elOutput = cart_product(elInput);
- // mostrar el resultado
- for (int i = 0; i < elOutput.size(); ++i) {
- for (int j = 0; j < elOutput[i].size(); ++j) {
- std::cout << elOutput[i][j] << " ";
- }
- std::cout << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement