Using an iterator to Divide an Array into Parts with Unequal Size
问题 I have an array which I need to divide up into 3-element sub-arrays. I wanted to do this with iterators, but I end up iterating past the end of the array and segfaulting even though I don't dereference the iterator . given: auto foo = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; I'm doing: auto bar = cbegin(foo); for (auto it = next(bar, 3); it < foo.end(); bar = it, it = next(bar, 3)) { for_each(bar, it, [](const auto& i) { cout << i << endl; }); } for_each(bar, cend(foo), [](const auto& i) { cout <<