Skip to content

Instantly share code, notes, and snippets.

@detunized
Created May 15, 2017 08:07
Show Gist options
  • Save detunized/f90f2afe31b6ed749753a15af28dce44 to your computer and use it in GitHub Desktop.
Save detunized/f90f2afe31b6ed749753a15af28dce44 to your computer and use it in GitHub Desktop.
Map a container to set and vector
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