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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:44:19+00:00 2026-06-14T10:44:19+00:00

I need to port a snippet written in Python to C++ but that snippet

  • 0

I need to port a snippet written in Python to C++
but that snippet is using combinations from itertools in python.

The line that I’m really interested to porting over to C++ is this one:

for k in combinations(range(n-i),2*i):

range(n-i) in Python will generate a list from 0 to (n-i) - 1

Let n = 16, i = 5

print range(n-i)

outputs:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

and python combinations will generate all possible combinations in that list.

e.g.

print list(combinations(range(n-i),2*i))

outputs:

[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 10),
(0, 1, 2, 3, 4, 5, 6, 7, 9, 10),
(0, 1, 2, 3, 4, 5, 6, 8, 9, 10),
(0, 1, 2, 3, 4, 5, 7, 8, 9, 10),
(0, 1, 2, 3, 4, 6, 7, 8, 9, 10),
(0, 1, 2, 3, 5, 6, 7, 8, 9, 10),
(0, 1, 2, 4, 5, 6, 7, 8, 9, 10),
(0, 1, 3, 4, 5, 6, 7, 8, 9, 10),
(0, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]

I want to generate similar output using std::vector and next_permutation from C++ but I’m still getting erroneous results. This is my current approach:

for(int j = 0; j < n-i; j++) {
        temp_vector.push_back(j); 
}

That snippet is equivalent to range(n-i) in Python.

But the following snippet:

do {
     myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;

Is not equivalent to combinations(range(n-i),2*i)) in Python, and I’ve tried many variations and still haven’t been able to come up with the results I’m expecting.

For example:

Let n = 16
i = 5

Python

>>> print len(list(combinations(range(n-i),2*i)))

11

C++

#include <vector>
#include <iostream>

using namespace std;
int main() {
    vector<int> temp_vector;
    vector< vector<int> > myvector;
    int n = 16, i = 5;
    for(int j = 0; j < n - i; j++) {
            temp_vector.push_back(j);
    }
    do {
            myvector.push_back(temp_vector);
    } while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));
    cout<<myvector.size()<<endl;
    return 0;
}

g++ combinations.cpp

./a.out

3628800

Any guidance will be greatly appreciated! Thanks a lot!

  • 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-14T10:44:20+00:00Added an answer on June 14, 2026 at 10:44 am

    combinations and permutations are not the same thing.

    A combination is an unordered list of a subset of the items from another set. A permutation is a unique order of the items in the list.

    You’re generating all combinations of 10 things from a list of 11 things, so you’ll get 11 results, each one missing a different one of the original 11 items.

    Generating every permutation will generate every unique order of the original 11 items. Since the items in this case are all unique that means the result would be 11! lists where each contains all 11 items. You’re only generating permutations from the first 10 items however, so you’re getting 10! lists, none of which contain the 11th item.

    You need to find an algorithm for generating combinations instead of permutations.


    There’s no built-in algorithm for combinations. std::next_permutation can be used as part of an algorithm to generate combinations: See Generating combinations in c++.

    Here’s an old draft proposal for algorithms for combinations, including code.

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

Sidebar

Related Questions

I need to port some code from python (a prototype which i haven't written),
I have application that use mysql database, but now i need to port it
I need to port my app from iPhone to blackberry, but I'm new in
I have a random line generator program written in Python2, but I need to
I need to port the following from the ASP.NET MVC 2 sourcecode from C#
I need to port some code from FreePascal to C. I'm a professional C
I have a Django app that use a django-tagging. I need to port this
I need to generate a random port number between 2000-65000 from a shell script.
I need to Determine the serial port name connected to other machine using c#.
we have this old application that we need to port to a new machine

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.