Better way for combination and permutation Vector?

I found this code, here: http://stackoverflow.com/questions/12991758/creating-all-possible-k-combinations-of-n-items-in-c
on line 11 I am getting an error saying do_sth//undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
is there a better way to do this?
vector<int> selectedNumbers;
	int n = 10;//0-9
	int k = numbers.size();
	fill(numbers.begin(), numbers.begin() + k, 1);
	do {
		for (int i = 0; i < n; i++) {
			if (numbers[i]) {
				selectedNumbers.push_back(i);
			}
		}
		do_sth(selectedNumbers);
		copy(selectedNumbers.begin(), selectedNumbers.end(), ostream_iterator<int>(cout, " "));
		cout << endl;
		selectedNumbers.clear();
	}
	while (prev_permutation(numbers.begin(), numbers.end())) {

	};
It's a function. You don't appear to have provided a definition for it. The compiler will complain about that as it can't generate code to call a function is knows nothing about.
so as in definition you mean a prototype?
@kbw if you go to the link i provided, i just did what they did
I looked, it doesn't make any sense to me.

Why don't you do it yourself without using that proposed solution? You can start here http://en.cppreference.com/w/cpp/algorithm/prev_permutation
Last edited on
@kbw if you go to the link i provided, i just did what they did

If you read what's above the code you blindly copied, they mention putting working code in do_sth which appears to be shorthand in some circles for do_something.
@kbw Thanks but i know how to implement permutation, but its combination that I am having a tough time with. my assignment is for us to use Brute Force for ex.
we have 0-9 with a combination of lets say any 5 elements. {0 1 2 3 4} {4 7 8 9 0} etc .combination will go through all possible combinations of 5 elements using 0-9. then for each combination i have to use permutation.


The code you have above is fine for generating the combinations. Just comment out the do_sth line and uncomment (from the original code) the lines that involved the variable combinations. At the end of the do-while loop you will have all of the combinations in the vector generated

... or ...

replace do_sth with the code that generates your permutations from a single combination.
Last edited on
If i wanted to test each permutation, by calling another method.
Would I call that method after selectedNumbers.clear();
Topic archived. No new replies allowed.