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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:09:13+00:00 2026-06-18T20:09:13+00:00

I have an integer vector of size n. e.g. std::vector<int> vec; vec.pushback(0); vec.pushback(1); vec.pushback(2);

  • 0

I have an integer vector of size “n”.

e.g.
std::vector<int> vec;
vec.pushback(0);
vec.pushback(1);
vec.pushback(2);
vec.pushback(3);
....

Now i want to generate all possible combinations of size = {0, 1, 2, … , n}.

please keep in mind that
{0, 1, 3} is not equal to {3, 1, 0} or {1, 0, 3} or {3, 0, 1}

Please help me if you have an idea.

Thanks in advance.

  • 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-18T20:09:14+00:00Added an answer on June 18, 2026 at 8:09 pm

    You could do something like this:

    #include<iostream>
    #include<algorithm>
    #include <vector>
    
        std::ostream& operator<< (std::ostream& os, std::vector<int> v)
        {
            for (auto it = v.begin(); it != v.end(); ++it)
            {
               os << *it << ",";
            }
            return os;
        }
    
    int main()
    {
       std::vector<int> v;
       v.push_back(1);
       v.push_back(2);
       v.push_back(3);
        do {
           std::cout << v;
        } while (std::next_permutation(v.begin(), v.end()));
    
    }
    

    As per @JamesKanze’s comment, this can only work if the vector is sorted to begin with, so if you have an unsorted vector, you should call std::sort on it first.

    Looking at this, it says:

    Transforms the range [first, last) into the next permutation from the set of all permutations that are lexicographically ordered with respect to operator< or comp.

    You can see it in action here

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

Sidebar

Related Questions

I have a simple integer vector, with only 4 values. I want to loop
How to resize a vector of vector containing integer values. std::vector<std::vecotr<int>> MyVector; int value
I have an integer column and I want to find numbers that start with
I have a vector of integers and I want to convert it to a
I have a matrix A with size (nr,nc), a vector of column indices B
I have downloaded an image and it is saved in a std::string . Now
I'm having trouble getting MPI_Gatherv to work with a std::vector. I have written a
I have a map which contains integer values. i want to re-arange this map
In my code, I have a vector <vector <vector <vector <std::tr1::shared_ptr<foo> > > >
I have a collection of about a hundred or so sorted vector<int> 's Although

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.