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 8969675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:37:34+00:00 2026-06-15T17:37:34+00:00

Given the following Python (from http://norvig.com/sudoku.html ) def cross(A, B): Cross product of elements

  • 0

Given the following Python (from http://norvig.com/sudoku.html)

def cross(A, B):
    "Cross product of elements in A and elements in B."
    return [a+b for a in A for b in B]

cols     = '123456789'
rows     = 'ABCDEFGHI'
squares  = cross(rows, cols)

This produces:

['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'B1', 'B2', 'B3', ...]

As an exercise, I want to do the same in C++. Currently I have:

#include <iostream>
#include <map>
#include <vector>

using std::string;
using std::vector;

static vector<string> cross_string(const string &A, const string &B)
{
    vector<string> result;

    for (string::const_iterator itA = A.begin(); itA != A.end(); ++itA) {
        for (string::const_iterator itB = B.begin(); itB != B.end(); ++itB) {
            char s[] = {*itA, *itB, 0};
            result.push_back(string(s));
        }
    }
    return result;
}

int main(int argc, char** argv)
{
    const char digits[] = "123456789";
    const char rows[]   = "ABCDEFGHI";

    vector<string> res = cross_string(rows, digits);

    for (vector<string>::const_iterator it = res.begin();
         it != res.end(); ++it) {
        std::cout << *it << std::endl;
    }
}

This works, but I was hoping there would be a better way. This also does only strings, whereas the python does any list…


Edit:

Thanks for all of the replies. I accepted the one that I understood best, but Alf’s answer was a close second. I note that all used C++11 and wonder whether as a novice at C++ I should adopt that directly instead of the learning the older standard. But that is perhaps best for another question.

  • 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-06-15T17:37:35+00:00Added an answer on June 15, 2026 at 5:37 pm

    Weirdly enough, cross_product is missing from the C++ algorithms library. It can easily be added but as Jerry’s and Alf’s answers show, opinions differ on how to do it best. In fact, I’d do it different still. Jerry’s interface conforms to that of the other C++ algorithms but he didn’t abstract away the cross product operation, which I’d do thusly:

    template <typename InputIt1,
              typename InputIt2,
              typename OutputIt,
              typename F>
    void cross_product(InputIt1 begin1,
                       InputIt1 end1,
                       InputIt2 begin2,
                       InputIt2 end2,
                       OutputIt out,
                       F f) {
        for (auto i = begin1; i != end1; ++i)
            for (auto j = begin2; j != end2; ++j)
                *out++ = f(*i, *j);
    }
    

    The call, in your example, would then look as follows:

    auto digits = "1234546789";
    auto chars = "ABCDEFGHI";
    vector<string> result;
    
    cross_product(digits, digits + strlen(digits),
                  chars, chars + strlen(chars),
                  back_inserter(result),
                  [](char a, char b) { return string() + a + b; });
    

    (Don’t I just love C++11? Yes, I do.)

    In a proper library I’d offer a second overload which supplies a default f operation which creates a tuple similar to what Jerry’s code does. It would even be thinkable to abstract this further to allow more than two ranges – after all, the Python list comprehension allows you to iterate over more than two ranges).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following the Python documentation for string.replace ( http://docs.python.org/library/string.html ): string.replace(str, old, new[, maxreplace]) Return
I am trying to run this snippet from http://www.ibm.com/developerworks/linux/library/l-prog3.html on a python 2.6 runtime.
Given the following string http://thedude.com/05/simons-cat-and-frog-100x100.jpg I would like to use substr or trim (or
The following python script allows me to scrape email addresses from a given file
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
Here's a simple python function that checks if a given url is valid: from
Following this guide: http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide I was not able to configure a Hello World WSGI
I have the following given html structure <li class=g> <div class=vsc> <div class=alpha></div> <div
Suppose it is the following structure given: from django.utils.translation import ugettext_lazy as _ #
I'm a little confused by the following example from the python documentation here .

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.