Generating all possible combinations of a vector elements.

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.
Last edited on
What do you have so far? If you post your code, we can critique it and give you help.

By the way, there a couple of questions about the assignment (the "requirements") that will affect the final solution.

1. Is the original vector allowed to have duplicate values?
2. Are values in the original vector allowed to be in the solution sub-sets multiple times or only once each (i.e. is {0, 1, 1} expected?)
If you need only permutations of one fixed size then it can be done the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
	std::vector<int> v = { 0, 1, 2, 3 };

	do
	{
		for ( auto x : v ) std::cout << x << ' ';
		std::cout << std::endl;
	} while ( std::next_permutation( v.begin(), v.end() ) );

	return 0;
}
Last edited on
I pasted vlad's code and it doesn't work in code blocks
I pasted vlad's code and it doesn't work in code blocks


As with the code in the lounge, you probably need to update your compiler or enable C++11 support on the one you're using. (Codeblocks is not a compiler.)

"It doesn't work" is useless in terms of getting people the necessary information to assist you. In the future, provide the errors emitted by the compiler (or a description of what happens when you run the code if it does compile.)
Well. that the code will be compiled change statement

std::vector<int> v = { 0, 1, 2, 3 };

to the following sequence of statements

1
2
3
4
5
6
7
	std::vector<int> v;
	v.reserve( 4 );

	v.push_back( 0 );
	v.push_back( 1 );
	v.push_back( 2 );
	v.push_back( 3 );


Also the based on range for statement you should also substitute for the usual for statement

for ( std::vector<int>::size_type i = 0; i < v.size(); i++ ) std::cout << v[i] << ' ';
Last edited on
Actually, Code::Blocks hosts a bunch of compilers
Here's the list:

GNU GCC Compiler
Microsoft Visual c++ Toolkit 2003
Microsoft Visual c++ 2005/2008
Borland c++ Compiler (5.5,5.82)
Digital Mars compiler
OpenWatcom (W32) compiler
GNU GCC Compiler for MSP430
Cygwin GCC
LCC compiler
Intel c/c++ compiler
SDCC compiler
Tiny C compiler
GDC D compiler
Digital Mars D compiler
GNU ARM GCC Compiler
GNU ARV GCC Compiler
GNU GCC Compiler for PowerPC
GNU GCC Compiler for Tricore

Which one is the one that supports it?
greenleaf800073 wrote:
Actually, Code::Blocks hosts is capable of invoking a bunch of compilers


greenleaf800073 wrote:
Which one is the one that supports it?

Which compiler(s) do you have installed? If you downloaded Code::Blocks with a compiler, it will be GNU GCC Compiler.
Last edited on
Topic archived. No new replies allowed.