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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:38:00+00:00 2026-06-15T22:38:00+00:00

How do I use a permuted number outside while loop like to make comparisons..

  • 0

How do I use a permuted number outside while loop like to make comparisons.. Because I have checked, vector outside while is same as defined earlier.

void display(vector<int> vec){
    for (int i=0; i<vec.size();i++){
        cout<<vec[i];
    }
}
int main()
{
  vector <int> vec;
  vec.push_back(3);
  vec.push_back(2);
  vec.push_back(1);
  vec.push_back(4);
  sort(vec.begin(), vec.end());
  while (next_permutation( vec.begin(), vec.end() )){
  display(vec);
  cout<<endl;
}
  • 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-15T22:38:01+00:00Added an answer on June 15, 2026 at 10:38 pm

    The following complete program works fine both with Visual C++ and with g++:

    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <utility>
    using namespace std;
    
    template< class Item >
    wostream& operator<<( wostream& stream, vector<Item> const& v )
    {
        stream << "[";
        for( auto p = v.begin(); p != v.end();  ++p )
        {
            if( p != v.begin() ) { wcout << ", "; }
            wcout << *p;
        }
        stream << "]";
        return stream;
    }
    
    int main()
    {
        int const   data[]  = {1, 2, 3, 4};
        vector <int> vec( begin( data ), end( data ) ); 
        sort(vec.begin(),vec.end());
        do
        {
            wcout << vec << endl; 
        } while (next_permutation( vec.begin(), vec.end() ) );
    }
    

    The main code is identical to your code (it was copied and pasted, and just edited a teeny tiny little bit to add the data).

    So, given that the code works, what exactly is the problem?


    EDIT: the OP has now updated the question with different code, a while loop instead of a do loop, and the order of the calls to next_permutation and display changed so that it fails to display the original permutation.

    The new code given,

    void display(vector<int> vec){
        for (int i=0; i<vec.size();i++){
            cout<<vec[i];
        }
    }
    int main()
    {
      vector <int> vec;
      vec.push_back(3);
      vec.push_back(2);
      vec.push_back(1);
      vec.push_back(4);
      sort(vec.begin(), vec.end());
      while (next_permutation( vec.begin(), vec.end() )){
      display(vec);
      cout<<endl;
    }
    

    does not compile even after adding the requisite headers and a using namespace std;.

    While this is only due to a missing right brace, it would have been avoided by copying and pasting the code from a working version.

    That is also much less work to do. 🙂

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

Sidebar

Related Questions

Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
I have been using the encryption class of codeigniter (PHP framework) for a while
Use Elixir and have two entities -- Voter and Candidate -- with many to
use pthread_create to create limited number of threads running concurrently Successfully compile and run
use javascript or other method ? have any recommendation?
Use Eclipse Classic with ADT plugin. Tried to make project from existing example of
Use case: You have 2-3 files displayed in your MacVim window(s). You press ctrl+Q
Use scenario is pretty simple: I have a desktop only application that could be
use case example I have a servlet that is receiving login requests. If a
Use case: we have some project meta-data files which we want tracked, but are

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.