C++ std::map::find
Check whether a parcel is registered even if a driver has yet to be assigned.
A simple example
std::map::find
- Start with A = 2
- Do this Look for key A
- Result Present
Syntax
drivers.find(parcel) != drivers.end()
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonFind a parcel whose driver is still unassigned
A dispatch desk registers parcels before assigning drivers. Parcel P7 already has a record with an empty assignment, and the operator must distinguish it from a parcel that has never been registered.
#include <iostream>
#include <map>
#include <string>
#include <optional>
int main() {
std::map<std::string, std::optional<std::string>> drivers{{"P7", std::nullopt}};
if (drivers.find("P7") != drivers.end()) std::cout << "Registered";
return 0;
}
Registered
P7 appears in the register and the driver field is still unassigned.
Find an unassigned booking
A clinic already has booking B12, although a clinician has yet to be assigned. The desk checks whether the identifier has a record before creating one. Is the key already present?
Look for a second ticket
A counter has recorded ticket T2 and receives a query for T3. The desk checks whether the identifier has a record before creating one. Is the key already present?
What to watch for
The optional driver and the presence of the parcel key are separate questions. find works in C++17.