1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <list>
#include <string>
#include <algorithm>
template < typename T, typename A >
std::list<T,A> split_before( std::list<T,A>& lst, typename std::list<T,A>::const_iterator here )
{
std::list<T,A> second_list ;
// https://en.cppreference.com/w/cpp/container/list/splice
second_list.splice( second_list.begin(), lst, here, lst.end() ) ;
return second_list ;
}
template < typename T, typename A >
std::list<T,A> split_after( std::list<T,A>& lst, typename std::list<T,A>::const_iterator here )
{
if( here == lst.end() ) return {} ; // return empty list
else return split_before( lst, ++here ) ;
}
template < typename SEQ > void print( const SEQ& seq )
{
std::cout << "[ " ;
for( const auto& v : seq ) std::cout << v << ' ' ;
std::cout << "]\n" ;
}
int main()
{
std::list<std::string> a = { "Church", "Turing", "Ada", "Knuth", "Wirth", "Dijkstra",
"Neumann", "Kleene", "Minsky", "Ritchie", "Thomson", "Stroustrup" } ;
print(a) ;
std::cout << "\nsplit after Dijkstra:\n" ;
auto b = split_after( a, std::find( a.begin(), a.end(), "Dijkstra" ) ) ;
print(a) ;
print(b) ;
std::cout << "\nsplit second list before Ritchie:\n" ;
const auto c = split_before( b, std::find( b.begin(), b.end(), "Ritchie" ) ) ;
print(a) ;
print(b) ;
print(c) ;
}
|