diff --git a/alib2std/src/extensions/container/map.hpp b/alib2std/src/extensions/container/map.hpp index 80c4fc087a5f27062d8281792dafd33631abdce1..d8b670d5b5430b42bb3b7f4fafce3f62d3f61c99 100644 --- a/alib2std/src/extensions/container/map.hpp +++ b/alib2std/src/extensions/container/map.hpp @@ -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. diff --git a/alib2std/test-src/extensions/container/MapTest.cpp b/alib2std/test-src/extensions/container/MapTest.cpp index d61eca037ed45497254c93d6de8eb316c4e8172a..f3990ffad649cc67ef646713406c1ed857c3f120 100644 --- a/alib2std/test-src/extensions/container/MapTest.cpp +++ b/alib2std/test-src/extensions/container/MapTest.cpp @@ -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" ); + } + } }