#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vect { 0, 1, 2, 3, 5, 8 };
// List of subvectors extracted from 'vect'.
// Initially populated with a single vector containing
// the first element from 'vect'.
//
std::vector<std::vector<int>> sub_vectors(1, std::vector<int>(1, vect[0]));
// Iterate over the elements of 'vect',
// skipping the first as it has already been processed.
//
std::for_each(vect.begin() + 1,
vect.end(),
[&](int i)
{
// It the current int is one more than previous
// append to current sub vector.
if (sub_vectors.back().back() == i - 1)
{
sub_vectors.back().push_back(i);
}
// Otherwise, create a new subvector contain
// a single element.
else
{
sub_vectors.push_back(std::vector<int>(1, i));
}
});
for (auto const& v: sub_vectors)
{
for (auto i: v) std::cout << i << ", ";
std::cout << std::endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKaW50IG1haW4oKQp7CiAgICBzdGQ6OnZlY3RvcjxpbnQ+IHZlY3QgeyAwLCAxLCAyLCAzLCA1LCA4IH07CgogICAgLy8gTGlzdCBvZiBzdWJ2ZWN0b3JzIGV4dHJhY3RlZCBmcm9tICd2ZWN0Jy4KICAgIC8vIEluaXRpYWxseSBwb3B1bGF0ZWQgd2l0aCBhIHNpbmdsZSB2ZWN0b3IgY29udGFpbmluZwogICAgLy8gdGhlIGZpcnN0IGVsZW1lbnQgZnJvbSAndmVjdCcuCiAgICAvLwogICAgc3RkOjp2ZWN0b3I8c3RkOjp2ZWN0b3I8aW50Pj4gc3ViX3ZlY3RvcnMoMSwgc3RkOjp2ZWN0b3I8aW50PigxLCB2ZWN0WzBdKSk7CgogICAgLy8gSXRlcmF0ZSBvdmVyIHRoZSBlbGVtZW50cyBvZiAndmVjdCcsCiAgICAvLyBza2lwcGluZyB0aGUgZmlyc3QgYXMgaXQgaGFzIGFscmVhZHkgYmVlbiBwcm9jZXNzZWQuCiAgICAvLwogICAgc3RkOjpmb3JfZWFjaCh2ZWN0LmJlZ2luKCkgKyAxLAogICAgICAgICAgICAgICAgICB2ZWN0LmVuZCgpLAogICAgICAgICAgICAgICAgICBbJl0oaW50IGkpCiAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgIC8vIEl0IHRoZSBjdXJyZW50IGludCBpcyBvbmUgbW9yZSB0aGFuIHByZXZpb3VzCiAgICAgICAgICAgICAgICAgICAgICAvLyBhcHBlbmQgdG8gY3VycmVudCBzdWIgdmVjdG9yLgogICAgICAgICAgICAgICAgICAgICAgaWYgKHN1Yl92ZWN0b3JzLmJhY2soKS5iYWNrKCkgPT0gaSAtIDEpCiAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgc3ViX3ZlY3RvcnMuYmFjaygpLnB1c2hfYmFjayhpKTsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgIC8vIE90aGVyd2lzZSwgY3JlYXRlIGEgbmV3IHN1YnZlY3RvciBjb250YWluCiAgICAgICAgICAgICAgICAgICAgICAvLyBhIHNpbmdsZSBlbGVtZW50LgogICAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgIHN1Yl92ZWN0b3JzLnB1c2hfYmFjayhzdGQ6OnZlY3RvcjxpbnQ+KDEsIGkpKTsKICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfSk7CgogICAgZm9yIChhdXRvIGNvbnN0JiB2OiBzdWJfdmVjdG9ycykKICAgIHsKICAgICAgICBmb3IgKGF1dG8gaTogdikgc3RkOjpjb3V0IDw8IGkgPDwgIiwgIjsKICAgICAgICBzdGQ6OmNvdXQgPDwgc3RkOjplbmRsOwogICAgfQp9Cg==