0

I wrote a piece of code to print all sub arrays using Iterators, from this [example solution][1] [1]: https://stackoverflow.com/questions/421573/best-way-to-extract-a-subvector-from-a-vector

void printarray(std::vector<int> array) {
    for (auto a : array) {
        std::cout << a <<" ";
    }
    std::cout<< std::endl;
}

void subArrayPrinter(vector<int>& arr) {
    for (int i = 0; i < arr.size(); i++) {
        for (int j = 2; j <= arr.size(); j++) {
            std::vector<int> newVec(arr.cbegin() + i, arr.cbegin() + j);
            printarray(newVec);
        }
    }
}

int main() {
    std::vector<int> zarray = {23, 2, 4, 6, 7};
    std::cout << subArrayPrinter(zarray) << std::endl;
}

for an example [23, 2, 4, 6, 7] I see a crash eventually which complains of array too big at the end

o/p

23 2 4 6 7 
23 2 
23 2 4 
23 2 4 6 
23 2 4 6 7 
2 
2 4 
2 4 6 
2 4 6 7 
        // <- the array size is zero here, but didnt see this for the first sub array case 
4 
4 6 
4 6 7 
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()
Aborted (core dumped)

I checked in gdb, for some reason the array size is getting huge

ks1322
  • 33,961
  • 14
  • 109
  • 164
ajax_velu
  • 286
  • 3
  • 16
  • 2
    `std::cout << subArrayPrinter(zarray)` How can this compile? `subArrayPrinter` returns `void`. `(arr.cbegin() + i, arr.cbegin() + j)` What do you think happens when `i` is greater then `j`? What do you intend to happen when `i` is greater then `j`? In the sperit of this forum - what is your question? Why are you writing that here? Do you have a question to ask? – KamilCuk Jul 25 '20 at 08:05
  • @KamilCuk `i` can't be greater than `j`, `i` goes to `< arr.size()` and `j` goes to `<=`. – Manuel Jul 26 '20 at 08:54
  • 1
    ? It can be, `i = 3` and `j = 2`. `< arr.size() and j goes to <=` yes, so both `i` and `j` are lower then `arr.size()`. Yet, `i` can be greater then `j` and both can be lower then `arr.size()`. On the 4th time the `for (int i = 0; i < arr.size(); i++)` loop enters, then `i` will be `3`, yet `j` will be `2`, ergo `j` will be lower then `i`. – KamilCuk Jul 26 '20 at 09:11
  • @KamilCuk you are right!. +1 for make me see my mistake! – Manuel Jul 26 '20 at 09:22

2 Answers2

0

As @KamilCuk rightly pointed out problems with your code. Reason of the crash is allocation of new vector when i > j. Probably you need to change the inner for loop to

for (int j = i + 1; j <= arr.size(); j++)
Vijay
  • 778
  • 4
  • 9
0

Assign i to j in the inner loop and you will see what you expect:

void subArrayPrinter(vector<int>& arr) {
    for (int i = 0; i < arr.size(); i++) {
        for (int j = i; j <= arr.size(); j++) {
            std::vector<int> newVec(arr.cbegin() + i, arr.cbegin() + j);
            printarray(newVec);
        }
    }
}

int main() {
    std::vector<int> zarray = {23, 2, 4, 6, 7};
    std::cout << "Begin " << endl;
    subArrayPrinter(zarray);
}

Outuput:

Begin 

23 
23 2 
23 2 4 
23 2 4 6 
23 2 4 6 7 

2 
2 4 
2 4 6 
2 4 6 7 

4 
4 6 
4 6 7 

6 
6 7 

7 
Manuel
  • 2,526
  • 1
  • 13
  • 17