[Heck ... als ich die Antwort geschrieben, die C++
Tag ging weg]
[mehr Beispiele Edited, einschließlich der Verwendung von char]
Kommentare im Code:
#include <vector>
// Function that recursively does the actual job
template <typename T, typename Function> void doCombinations(
size_t num, const std::vector<T>& values,
size_t start, std::vector<T>& combinationSoFar,
Function action
) {
if(0==num) { // the entire combination is complete
action(combinationSoFar);
}
else {
// walk through with the current position to the right,
// taking care to let enough walking room for the rest of the elements
for(size_t i=start; i<values.size()+1-num; i++) {
// push the current value there
combinationSoFar.push_back(values[i]);
// recursive call with one less element to enter combination
// and one position to the right for the next element to consider
doCombinations(num-1, values, i+1, combinationSoFar, action);
// pop the current value, we are going to move it to the right anyway
combinationSoFar.pop_back();
}
}
}
// function for the user to call. Prepares everything needed for the
// doCombinations
template <typename T, typename Function>
void for_each_combination(
size_t numInCombination,
const std::vector<T>& values,
Function action
) {
std::vector<T> combination;
doCombinations(numInCombination, values, 0, combination, action);
}
// dummy do-something with the vector
template <typename T> void cout_vector(const std::vector<T>& v) {
std::cout << '[';
for(size_t i=0; i<v.size(); i++) {
if(i) {
std::cout << ",";
}
std::cout << v[i];
}
std::cout << ']' << std::endl;
}
// Assumes the T type supports both addition and ostream <<
template <typename T> void adder(const std::vector<T>& vals) {
T sum=static_cast<T>(0);
for(T v : vals) {
sum+=v;
}
std::cout << "Sum: " << sum << " for ";
cout_vector(vals);
}
int main() {
std::cout << "Char combinations" << std::endl;
std::vector<char> char_vals{'A', 'B', 'C', 'D', 'E'};
for_each_combination(3, char_vals, cout_vector<char>);
std::cout << "\nInt combinations" << std::endl;
std::vector<int> int_vals{0, 1, 2, 3, 4};
for_each_combination(3, int_vals, cout_vector<int>);
std::cout <<"\nFloat combination adder" << std::endl;
std::vector<float> float_vals{0.0, 1.1, 2.2, 3.3, 4.4};
for_each_combination(3, float_vals, adder<float>);
return 0;
}
Ausgang:
Char combinations
[A,B,C]
[A,B,D]
[A,B,E]
[A,C,D]
[A,C,E]
[A,D,E]
[B,C,D]
[B,C,E]
[B,D,E]
[C,D,E]
Int combinations
[0,1,2]
[0,1,3]
[0,1,4]
[0,2,3]
[0,2,4]
[0,3,4]
[1,2,3]
[1,2,4]
[1,3,4]
[2,3,4]
Float combination adder
Sum: 3.3 for [0,1.1,2.2]
Sum: 4.4 for [0,1.1,3.3]
Sum: 5.5 for [0,1.1,4.4]
Sum: 5.5 for [0,2.2,3.3]
Sum: 6.6 for [0,2.2,4.4]
Sum: 7.7 for [0,3.3,4.4]
Sum: 6.6 for [1.1,2.2,3.3]
Sum: 7.7 for [1.1,2.2,4.4]
Sum: 8.8 for [1.1,3.3,4.4]
Sum: 9.9 for [2.2,3.3,4.4]
I braucht keine Permutation ich brauche Kombination. AB und BA sind für mich gleich. – omixam
Ups, falsch gelesen. Ich war mir nicht ganz sicher, was Sie mit "Combinatorics" meinten. –
Wird der Name in der Mathematik verwendet, um von Permutation zu unterscheiden. – omixam