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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:46:10+00:00 2026-05-11T20:46:10+00:00

Ok, so I wrote an stl-like algorithm called cartesian_product . For those who don’t

  • 0

Ok, so I wrote an stl-like algorithm called cartesian_product. For those who don’t know, the cartesian product is every possible pair of elements from two sets. So the cartesian product of {1, 2, 3} and {10, 20, 30} is

{(1,10), (1,20), (1,30), (2,10), (2,20), (2,30), (3,10), (3,20), (3,30)}

So the function looks like

template <typename InIt1, typename InIt2, typename OutIt>
void
cartesian_product(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt out)
{
    for (; first1 != last1; ++first1)
        for (InIt2 it = first2; it != last2; ++it)
            *out++ = std::make_pair(*first1, *it);
}

There aren’t template typedefs, so I made a traits class to hold the type that
the output iterator is from:

template <typename ObjA, typename ObjB, template <typename> class Container>
struct cartesian_product_traits
{
    typedef Container<std::pair<ObjA, ObjB> > type;
};

So then I can say:

typedef cartesian_product_traits<int, int, std::vector>::type IntPairList;
IntPairList my_list;
cartesian_product(v1.begin(), v1.end(), 
                  v2.begin(), v2.end(),
                  std::back_inserter(my_list);

but this doesn’t seem to compile. I get a nice error:

error C3201: the template parameter list for class template 'std::vector' does not match the template parameter list for template parameter 'Container'

So I’m stumped. How do I get this to work?

  • 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-11T20:46:10+00:00Added an answer on May 11, 2026 at 8:46 pm

    The template parameter list for vector isn’t just one element, it takes two:

    template < class T, class Allocator = allocator<T> > class vector
    

    so in order to accept vector, you need to have a template template parameter with two blanks:

    template <typename ObjA, typename ObjB, template <typename, typename> class Container>
    struct cartesian_product_traits
    

    Edited: cut some advice, misread your code.

    The way to do this properly would be to use a variadic macro on the template template parameter:

    template <typename ObjA, typename ObjB, template <typename ...> class Container>
    struct cartesian_product_traits
    

    But that’s far away from being a standard. If it were my code, I’d probably just have the consumers pound out the full template:

    std::vector< std::pair<int, int> >
    

    is shorter than

    cartesian_product_traits<int, int, vector>
    

    and the latter would only help if the definition for a Cartesian product changed.

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My guess is that there are errors when the build… May 12, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer I think you can reference the oprofile's implementation. Oprofile use… May 12, 2026 at 12:14 am
  • Editorial Team
    Editorial Team added an answer Believe it or not, I don't think there's really enough… May 12, 2026 at 12:14 am

Related Questions

Ok, so I wrote an stl-like algorithm called cartesian_product . For those who don't
i've written a UserControl descendant that is in an assembly dll. How do i
Ok, so everyone has decided (and for good reason) strait SQL is of the
I just lost 50% of my answer on a test because I wrote the
Ok, the short version is that I have a Linq entity from a LinqDataSourceUpdateEventArgs

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.