Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4061300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:27:01+00:00 2026-05-20T15:27:01+00:00

I’ve a vector of vectors say vector<vector<int>> of different sizes as follows: vector<vector<int>> items

  • 0

I’ve a vector of vectors say vector<vector<int>> of different sizes as follows:

vector<vector<int>> items = {
    { 1, 2, 3 },
    { 4, 5 },
    { 6, 7, 8 }
};

I want to create combinations in terms of Cartesian product of these vectors like

1,4,6
1,4,7
1,4,8
1,5,6
// ...
3,5,7
3,5,8

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:27:01+00:00Added an answer on May 20, 2026 at 3:27 pm

    First, I’ll show you a recursive version.

    typedef std::vector<int> Vi;
    typedef std::vector<Vi> Vvi;
    
    // recursive algorithm to to produce cart. prod.
    // At any given moment, "me" points to some Vi in the middle of the
    // input data set. 
    //   for int i in *me:
    //      add i to current result
    //      recurse on next "me"
    void cart_product(
        Vvi& rvvi,  // final result
        Vi&  rvi,   // current result 
        Vvi::const_iterator me, // current input
        Vvi::const_iterator end) // final input
    {
        if(me == end) {
            // terminal condition of the recursion. We no longer have
            // any input vectors to manipulate. Add the current result (rvi)
            // to the total set of results (rvvvi).
            rvvi.push_back(rvi);
            return;
        }
    
        // need an easy name for my vector-of-ints
        const Vi& mevi = *me;
        for(Vi::const_iterator it = mevi.begin(); it != mevi.end(); it++) {
            // final rvi will look like "a, b, c, ME, d, e, f"
            // At the moment, rvi already has "a, b, c"
            rvi.push_back(*it);  // add ME
            cart_product(rvvi, rvi, me+1, end); add "d, e, f"
            rvi.pop_back(); // clean ME off for next round
        }
    }
    

    See full code at Compiler Explorer.

    Now, I’ll show you the recursive iterative version that I shamelessly stole from @John :

    // Seems like you'd want a vector of iterators
    // which iterate over your individual vector<int>s.
    struct Digits {
        Vi::const_iterator begin;
        Vi::const_iterator end;
        Vi::const_iterator me;
    };
    typedef std::vector<Digits> Vd;
    void cart_product(
        Vvi& out,  // final result
        Vvi& in)  // final result
     
    {
        Vd vd;
        
        // Start all of the iterators at the beginning.
        for(Vvi::const_iterator it = in.begin();
            it != in.end();
            ++it) {
            Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
            vd.push_back(d);
        }
        
        while(1) {
            // Construct your first product vector by pulling 
            // out the element of each vector via the iterator.
            Vi result;
            for(Vd::const_iterator it = vd.begin();
                it != vd.end();
                it++) {
                result.push_back(*(it->me));
            }
            out.push_back(result);
    
            // Increment the rightmost one, and repeat.
    
            // When you reach the end, reset that one to the beginning and
            // increment the next-to-last one. You can get the "next-to-last"
            // iterator by pulling it out of the neighboring element in your
            // vector of iterators.
            for(Vd::iterator it = vd.begin(); ; ) {
                // okay, I started at the left instead. sue me
                ++(it->me);
                if(it->me == it->end) {
                    if(it+1 == vd.end()) {
                        // I'm the last digit, and I'm about to roll
                        return;
                    } else {
                        // cascade
                        it->me = it->begin;
                        ++it;
                    }
                } else {
                    // normal
                    break;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.