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 !!!


#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) {

  // Input: S is a sorted vector
  CombinationsGenerator(std::vector<int> const &S) : S(S) {

  void generate() {

int main() {
  int S_i[] = {1, 2, 3, 5};
  CombinationsGenerator(std::vector<int>(S_i, S_i+4)).generate();

When I run your code in Code::Blocks 12.11, it seems run fine I get this output:

1 3
1 3 5
1 5
2 5
3 5

I get the same error though, when I run the code in VS2010. I think it might have something to do with the header file for vector used in VS.

I would suggest that if your not addicted to Intellisense or not making a forms projects to try something like Code::Blocks, especially if you want to think about portability down the road. An IDE that is truer to c++ standards will make it easier to port your code to other OS's. Microsoft has proven time and again that instead of implementing a standard as given, they have to pervert it in order to fit their idea of what it should be.

Your algorithm will recurse on line 34, adding a new element to combination until it contains {0, 1, 2, 3}, since that's the size of S. On the recursion after the 3 is added, last, on line 29, will contain the final element of combination (3). On line 30 you then try to access S[last +1], but since S only has 4 elements, this isn't defined. The version of STL in VS has a whole bunch of debug checks to make sure that this kind of thing is caught. My guess is that if you run in release, the program will (incorrectly) run and may or may not produce garbage.