Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm> // std::sort
- #include <iostream> // std::cout
- #include <iterator> // output_iterator
- #include <utility> // std::begin, std::end
- #include <vector> // std::vector
- using namespace std;
- template< class ResultCollection, class C >
- ResultCollection create( C const& c )
- { return ResultCollection( begin( c ), end( c ) ); }
- template< class Type >
- class Ref
- {
- private:
- Type* p_;
- public:
- Type& ref() const { return *p_; }
- Ref( Type& r ): p_( &r ) {}
- };
- template< class Type >
- Ref< Type > ref( Type& r ) { return Ref< Type >( r ); }
- template< class C, class... Args >
- class With
- {
- private:
- Ref< C > c_;
- public:
- typedef decltype( begin( declval<C&>() ) ) Iter;
- void operator()( void f( Iter, Iter, Args... ), Args&&... args )
- { return f( begin( c_.ref() ), end( c_.ref() ), forward( args )... ); }
- template< class OutIter >
- OutIter operator()(
- OutIter f( Iter, Iter, OutIter, Args... ),
- OutIter const outIt,
- Args&&... args
- )
- { return f( begin( c_.ref() ), end( c_.ref() ), outIt, forward( args )... ); }
- With( C& c )
- : c_( c )
- {}
- };
- template< class C >
- With< C > with( C& c ) { return With< C >( c ); }
- int main()
- {
- int const data[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 4};
- auto v = create< vector< int > >( data );
- with( v )( sort );
- with( v )( copy, ostream_iterator<int>( cout, " " ) );
- }
Add Comment
Please, Sign In to add comment