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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:20:07+00:00 2026-05-11T18:20:07+00:00

I am currently writing code in C++ to find all possible permutations of 6

  • 0

I am currently writing code in C++ to find all possible permutations of 6 integers and store the best permutation (i.e. the one whose total is closest to a given value).

I am trying to write this code as efficiently as possible and would apreciate any advice or examples.

I was considering storing the integers in an array and performing the permutations using pointers within a loop. Is this a good approach?

  • 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-11T18:20:08+00:00Added an answer on May 11, 2026 at 6:20 pm

    They already thought of this one for you:

    #include <algorithm>
    
    int ra[6] = { 1, 2, 3, 4, 5, 6 };
    
    do {
        // whatever
    } while (std::next_permutation(ra, ra+6));
    

    Note that the elements have to start in increasing order (by comparison via operator<), or else the loop will terminate before you’ve seen every permutation. See http://en.cppreference.com/w/cpp/algorithm/next_permutation for details.

    It probably doesn’t give the fastest runtime possible, because at each step it has to examine the array to work out what to change. But it’s certainly the most efficient in programmer effort, and without knowing the compiler and platform it’s not really possible to micro-optimise the nested loop approaches anyway.

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

Sidebar

Related Questions

I'm currently re-writing one of my old PHP scripts so that the code is
I'm currently writing a piece of code that does some searches which returns IDisposable
I'm currently writing a billing application using EF 5 Code First, and I'm running
I'm currently writing a MEX file in C to speed up MATLAB code that
I am currently writing a formal research report, and I'll be including code with
I am writing some c# code that is currently supposed to run as fast
I'm writing a compiler which uses C as an intermediate code which is (Currently)
I'm currently writing a simple game. My graphics code runs once per frame (approximately
I'm writing a set of test scenarios for my code, and currently I have
In my current project, I am writing code generator. The interface would be Command

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.