#include #include #include #include using namespace std ; // username with id map students_db = { {"John Kerr", 12501 }, {"Fred Louis", 32502 }, {"David Marlowe", 42503 } }; optional get_student_id( const string& name ) { auto it = students_db.find( name ) ; if ( it != students_db.end()) { return optional(it->second) ; } return nullopt; // Explicitly returns "nothing" } optional get_student_id1( const string& name ) { auto it = students_db.find( name ) ; if ( it != students_db.end()) { return it->second ; } return {} ; // Explicitly returns "nothing" } int main() { string student_name = "John Kerr" ; optional id = get_student_id( student_name ) ; // 1. Check if the optional contains a value using contextual bool conversion //optional can be used as a condition and will produce true if //it contains a valid value if ( id ) { // 2. Access the value safely using the * operator cout << student_name << "'s id is: " << *id << endl; } else { cout << student_name << " was not found." << endl; } //Another way if ( id.has_value() ) { // 2. Access the value safely using the * operator cout << student_name << "'s id is : " << id.value() << endl; } else { cout << student_name << " was not found." << endl; } student_name = "Charles Bronson" ; //Use the second function. id = get_student_id1( student_name ) ; cout << student_name << " id is: " << id.value_or(0) << endl; }