Quantcast
Viewing latest article 3
Browse Latest Browse All 16

Answer by WBuck for How to find if a given key exists in a std::map

C++17 simplified this a bit more with an If statement with initializer.This way you can have your cake and eat it too.

if ( auto it{ m.find( "key" ) }; it != std::end( m ) ) {    // Use `structured binding` to get the key    // and value.    const auto&[ key, value ] { *it };    // Grab either the key or value stored in the pair.    // The key is stored in the 'first' variable and    // the 'value' is stored in the second.    const auto& mkey{ it->first };    const auto& mvalue{ it->second };    // That or just grab the entire pair pointed    // to by the iterator.    const auto& pair{ *it };} else {   // Key was not found..}

Viewing latest article 3
Browse Latest Browse All 16

Trending Articles