Created
May 15, 2017 08:07
-
-
Save detunized/f90f2afe31b6ed749753a15af28dce44 to your computer and use it in GitHub Desktop.
Map a container to set and vector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template < typename Container, typename Function > | |
auto map_to_set( const Container& c, Function f ) -> std::set< decltype( f( *c.begin( ) ) ) > | |
{ | |
std::set< decltype( f( *c.begin( ) ) ) > result; | |
for ( auto&& i : c ) | |
result.insert( f( i ) ); | |
return result; | |
} | |
template < typename Container, typename Function > | |
auto map_to_vector( const Container& c, Function f ) -> std::vector< decltype( f( *c.begin( ) ) ) > | |
{ | |
std::vector< decltype( f( *c.begin( ) ) ) > result; | |
result.reserve( c.size( ) ); | |
for ( auto&& i : c ) | |
result.emplace_back( f( i ) ); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment