#include <iostream>
#include <list>
#include <vector>
std::vector< std::list<int> > & split( const std::list<int> & v,
int delim, std::vector< std::list<int> >& elems) {
int i = 0;
std::list<int>::const_iterator it = v.begin();
while ( it != v.end()) {
std::list<int> l;
std::list<int>::const_iterator it3 = l.begin();
while ( it != v.end() && *it < delim) {
l.splice( it3++, v, it++ );
}
if( it != v.end()) l.splice( it3++, v, it++ );
elems.push_back( l);
}
return elems;
}
std::vector< std::list<int> > split( const std::list<int> &v, int delim) {
std::vector< std::list<int> > elems;
split( v, delim, elems);
return elems;
}
int main() {
// your code goes here
std::list<int> v = { 1, 2, 3, 503, 5, 6, 502, 7, 510, 3, 500, 5, 9};
std::vector< std::list<int> > vl;
vl = split( v, 500);
int i = 0;
while( i < vl.size()) {
std::list<int>::const_iterator it = vl[ i].begin();
while( it != vl[ i].end())
std::cout << *it++;
++i;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bGlzdD4KI2luY2x1ZGUgPHZlY3Rvcj4KCnN0ZDo6dmVjdG9yPCBzdGQ6Omxpc3Q8aW50PiA+ICYgc3BsaXQoIGNvbnN0IHN0ZDo6bGlzdDxpbnQ+ICAmIHYsCiAgICAgICAgICAgICAgICAgICBpbnQgZGVsaW0sIHN0ZDo6dmVjdG9yPCBzdGQ6Omxpc3Q8aW50PiA+JiBlbGVtcykgewoKICAgIGludCBpID0gMDsKICAgIHN0ZDo6bGlzdDxpbnQ+Ojpjb25zdF9pdGVyYXRvciBpdCA9IHYuYmVnaW4oKTsKCiAgICB3aGlsZSAoIGl0ICE9IHYuZW5kKCkpIHsKICAgIAlzdGQ6Omxpc3Q8aW50PiBsOwogICAgCXN0ZDo6bGlzdDxpbnQ+Ojpjb25zdF9pdGVyYXRvciBpdDMgPSBsLmJlZ2luKCk7CiAgICAgICAgd2hpbGUgKCBpdCAhPSB2LmVuZCgpICYmICppdCA8IGRlbGltKSB7CiAgICAgICAgICAgIGwuc3BsaWNlKCBpdDMrKywgdiwgaXQrKyApOwogICAgICAgIH0KICAgICAgICBpZiggaXQgIT0gdi5lbmQoKSkgbC5zcGxpY2UoIGl0MysrLCB2LCBpdCsrICk7CiAgICAgICAgZWxlbXMucHVzaF9iYWNrKCBsKTsKICAgIH0KICAgIHJldHVybiBlbGVtczsKfQoKCnN0ZDo6dmVjdG9yPCBzdGQ6Omxpc3Q8aW50PiA+IHNwbGl0KCBjb25zdCBzdGQ6Omxpc3Q8aW50PiAgJnYsIGludCBkZWxpbSkgewogICAgc3RkOjp2ZWN0b3I8IHN0ZDo6bGlzdDxpbnQ+ID4gZWxlbXM7CiAgICBzcGxpdCggdiwgZGVsaW0sIGVsZW1zKTsKICAgIHJldHVybiBlbGVtczsKfQoKaW50IG1haW4oKSB7CgkvLyB5b3VyIGNvZGUgZ29lcyBoZXJlCglzdGQ6Omxpc3Q8aW50PiB2ID0geyAxLCAyLCAzLCA1MDMsIDUsIDYsIDUwMiwgNywgNTEwLCAzLCA1MDAsIDUsIDl9OwoJc3RkOjp2ZWN0b3I8IHN0ZDo6bGlzdDxpbnQ+ID4gdmw7Cgl2bCA9IHNwbGl0KCB2LCA1MDApOwoJCglpbnQgaSA9IDA7Cgl3aGlsZSggaSA8IHZsLnNpemUoKSkgewoJICAgIHN0ZDo6bGlzdDxpbnQ+Ojpjb25zdF9pdGVyYXRvciBpdCA9IHZsWyBpXS5iZWdpbigpOwoJCXdoaWxlKCBpdCAhPSAgdmxbIGldLmVuZCgpKQoJCSAgICBzdGQ6OmNvdXQgPDwgKml0Kys7CgkJKytpOwoJfQoJCglyZXR1cm4gMDsKfQ==
prog.cpp: In function ‘std::vector<std::list<int> >& split(const std::list<int>&, int, std::vector<std::list<int> >&)’:
prog.cpp:15:38: error: no matching function for call to ‘std::list<int>::splice(std::_List_const_iterator<int>::_Self, const std::list<int>&, std::_List_const_iterator<int>::_Self)’
l.splice( it3++, v, it++ );
^
prog.cpp:15:38: note: candidates are:
In file included from /usr/include/c++/4.8/list:63:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_list.h:1248:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x)
^
/usr/include/c++/4.8/bits/stl_list.h:1248:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1263:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x)
^
/usr/include/c++/4.8/bits/stl_list.h:1263:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1278:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x, iterator __i)
^
/usr/include/c++/4.8/bits/stl_list.h:1278:7: note: no known conversion for argument 1 from ‘std::_List_const_iterator<int>::_Self {aka std::_List_const_iterator<int>}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
/usr/include/c++/4.8/bits/stl_list.h:1296:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x, iterator __i)
^
/usr/include/c++/4.8/bits/stl_list.h:1296:7: note: no known conversion for argument 1 from ‘std::_List_const_iterator<int>::_Self {aka std::_List_const_iterator<int>}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
/usr/include/c++/4.8/bits/stl_list.h:1314:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&, std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x, iterator __first,
^
/usr/include/c++/4.8/bits/stl_list.h:1314:7: note: candidate expects 4 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1332:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&, std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x, iterator __first, iterator __last)
^
/usr/include/c++/4.8/bits/stl_list.h:1332:7: note: candidate expects 4 arguments, 3 provided
prog.cpp:17:53: error: no matching function for call to ‘std::list<int>::splice(std::_List_const_iterator<int>::_Self, const std::list<int>&, std::_List_const_iterator<int>::_Self)’
if( it != v.end()) l.splice( it3++, v, it++ );
^
prog.cpp:17:53: note: candidates are:
In file included from /usr/include/c++/4.8/list:63:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_list.h:1248:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x)
^
/usr/include/c++/4.8/bits/stl_list.h:1248:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1263:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x)
^
/usr/include/c++/4.8/bits/stl_list.h:1263:7: note: candidate expects 2 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1278:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x, iterator __i)
^
/usr/include/c++/4.8/bits/stl_list.h:1278:7: note: no known conversion for argument 1 from ‘std::_List_const_iterator<int>::_Self {aka std::_List_const_iterator<int>}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
/usr/include/c++/4.8/bits/stl_list.h:1296:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x, iterator __i)
^
/usr/include/c++/4.8/bits/stl_list.h:1296:7: note: no known conversion for argument 1 from ‘std::_List_const_iterator<int>::_Self {aka std::_List_const_iterator<int>}’ to ‘std::list<int>::iterator {aka std::_List_iterator<int>}’
/usr/include/c++/4.8/bits/stl_list.h:1314:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&&, std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list&& __x, iterator __first,
^
/usr/include/c++/4.8/bits/stl_list.h:1314:7: note: candidate expects 4 arguments, 3 provided
/usr/include/c++/4.8/bits/stl_list.h:1332:7: note: void std::list<_Tp, _Alloc>::splice(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>&, std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::list<_Tp, _Alloc>::iterator = std::_List_iterator<int>]
splice(iterator __position, list& __x, iterator __first, iterator __last)
^
/usr/include/c++/4.8/bits/stl_list.h:1332:7: note: candidate expects 4 arguments, 3 provided
prog.cpp:8:9: warning: unused variable ‘i’ [-Wunused-variable]
int i = 0;
^
prog.cpp: In function ‘int main()’:
prog.cpp:37:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while( i < vl.size()) {
^