This is a program to generate possible combinations
Somehow everytime I run it - in VS2012 I get this error -
"Expression:Vector Subscript out of range" ?
Can someone please help to get this code working ?
A Big Thank You !!!
:D
#include <iostream>
#include <vector>
class CombinationsGenerator {
std::vector<int> S;
std::vector<int> combination;
void generate_recursive() {
for (int i = 0, e = combination.size(); i < e; ++i)
std::cout << S[combination[i]] << ' ';
std::cout << '\n';
int first;
if (combination.empty())
first = 0;
else {
int last = *combination.rbegin();
first = last + (S[last+1] == S[last] + 1 ? 2 : 1);
}
for (int i = first, end = S.size(); i < end; ++i) {
combination.push_back(i);
generate_recursive();
combination.pop_back();
}
}
public:
// Input: S is a sorted vector
CombinationsGenerator(std::vector<int> const &S) : S(S) {
}
void generate() {
combination.clear();
generate_recursive();
}
};
int main() {
int S_i[] = {1, 2, 3, 5};
CombinationsGenerator(std::vector<int>(S_i, S_i+4)).generate();
}
TheTimeCat 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.