Skip to content
Snippets Groups Projects
Commit 695e3676 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

overload map at with a default parameter

parent 55ae6162
No related branches found
No related tags found
1 merge request!81overload map at with a default parameter
Pipeline #35801 canceled
......@@ -163,6 +163,24 @@ public:
return insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
}
 
using std::map< T, R, Cmp, Alloc >::at;
R & at ( const T & key, R & defaultValue ) {
auto value = this->find ( key );
if ( value == end ( ) )
return defaultValue;
else
return value->second;
}
const R & at ( const T & key, const R & defaultValue ) const {
auto value = this->find ( key );
if ( value == end ( ) )
return defaultValue;
else
return value->second;
}
/**
* \brief
* Inherited behavior of begin for non-const instance.
......
......@@ -55,4 +55,23 @@ TEST_CASE ( "Map", "[unit][std][container]" ) {
 
CHECK ( copies == 0 );
}
SECTION ( "At" ) {
ext::map < int, std::string > map;
map.insert ( 1, "one" );
{
std::string defaultValue = "default";
std::string & value = map.at ( 0, defaultValue );
CHECK ( value == "default" );
}
{
const std::string defaultValue = "default";
const std::string & value = map.at ( 0, defaultValue );
CHECK ( value == "default" );
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment