Home >Backend Development >C++ >How to Implement a Cartesian Product of Vectors Using Recursion and Iteration?

How to Implement a Cartesian Product of Vectors Using Recursion and Iteration?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-08 17:11:12345browse

How to Implement a Cartesian Product of Vectors Using Recursion and Iteration?

Implementing Vector Cartesian Product with Recursion and Iteration

You have a vector of vectors items with varying sizes, and you seek to generate a Cartesian product of their elements.

First, a recursive approach can be employed:

// Recursive Cartesian product function
void cart_product(
    Vvi& rvvi,  // Result vector of vectors
    Vi& rvi,   // Current vector
    Vvi::const_iterator me, // Current vector in input
    Vvi::const_iterator end) // Input vector end
{
    if (me == end) {
        // Push the current result to the final result
        rvvi.push_back(rvi);
        return;
    }
    
    // Iterate through the current vector
    const Vi& mevi = *me;
    for (Vi::const_iterator it = mevi.begin(); it != mevi.end(); it++) {
        // Add current element to result
        rvi.push_back(*it);
        
        // Recurse to add subsequent elements
        cart_product(rvvi, rvi, me + 1, end);
        
        // Remove added element for further recursion
        rvi.pop_back();
    }
}

Now, consider the iterative approach:

// Iterative Cartesian product function
void cart_product(
    Vvi& out,  // Result vector of vectors
    Vvi& in)  // Input vector of vectors
{
    Vd vd; // Vector of iterator structs
    // Initialize iterators to start of vectors
    for (Vvi::const_iterator it = in.begin(); it != in.end(); ++it) {
        Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
        vd.push_back(d);
    }
    
    // Iterative loop to generate product vectors
    while (1) {
        // Create result vector from iterated elements
        Vi result;
        for (Vd::const_iterator it = vd.begin(); it != vd.end(); it++) {
            result.push_back(*(it->me));
        }
        out.push_back(result);
        
        // Increment iterator
        // Reset iterator at end, increment neighboring iterator
        for (Vd::iterator it = vd.begin(); ; ) {
            ++(it->me);
            if (it->me == it->end) {
                if (it + 1 == vd.end()) {
                    // End of product
                    return;
                } else {
                    // Cascade reset
                    it->me = it->begin;
                    ++it;
                }
            } else {
                // Break from loop
                break;
            }
        }
    }
}

The above is the detailed content of How to Implement a Cartesian Product of Vectors Using Recursion and Iteration?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn