patterncppMinor
Simple user registry code in C++
Viewed 0 times
codeusersimpleregistry
Problem
I'm learning C++, and would like more experienced folks to review my code, and point out what could be improved/changed.
Here's the problem that should be solved (taken from Kattis Securedoors)
You need to write a simple software auditor for tracking employees
entering and exiting your building using their access cards (each card
uniquely identifies the employee holding the card). Your software
needs to look at the access log and determine if there are any
anomalies. There are two types of anomalies your software should
detect:
already supposed to be in the building
when, according to the log, they are not supposed to be in the
building
When your software sees someone enter a building (even if
it’s an anomaly), that person is assumed to be inside the building
from that point on, until he exits. Similarly, if your software sees
someone exit the building (even if it’s an anomaly), that person is
assumed to not be in the building from that point on, until he enters
again. At the beginning of the log, everyone is assumed to be outside
the building.
Input
Input Input starts with a number 1≤n≤10001≤n≤1000 indicating the
length of the log. This is followed by nn lines, each line describing
either an entry or exit by an employee. Each description is of the
form ‘entry name’ or ‘exit name’, where name is a string of up to 20
uppercase and/or lowercase characters (a-z).
Output
For each person’s entry or exit, print the name of the person,
followed by
Sample Input 1
Sample Output 1
Here's my code (working sample)
```
#include
#include
#include
Here's the problem that should be solved (taken from Kattis Securedoors)
You need to write a simple software auditor for tracking employees
entering and exiting your building using their access cards (each card
uniquely identifies the employee holding the card). Your software
needs to look at the access log and determine if there are any
anomalies. There are two types of anomalies your software should
detect:
- a user entering the building when, according to the log, they are
already supposed to be in the building
- a user exiting the building
when, according to the log, they are not supposed to be in the
building
When your software sees someone enter a building (even if
it’s an anomaly), that person is assumed to be inside the building
from that point on, until he exits. Similarly, if your software sees
someone exit the building (even if it’s an anomaly), that person is
assumed to not be in the building from that point on, until he enters
again. At the beginning of the log, everyone is assumed to be outside
the building.
Input
Input Input starts with a number 1≤n≤10001≤n≤1000 indicating the
length of the log. This is followed by nn lines, each line describing
either an entry or exit by an employee. Each description is of the
form ‘entry name’ or ‘exit name’, where name is a string of up to 20
uppercase and/or lowercase characters (a-z).
Output
For each person’s entry or exit, print the name of the person,
followed by
entered or exited. If the action is anomalous, print(ANOMALY) afterward.Sample Input 1
8
entry Abbey
entry Abbey
exit Abbey
entry Tyrone
exit Mason
entry Demetra
exit Latonya
entry Idella
Sample Output 1
Abbey entered
Abbey entered (ANOMALY)
Abbey exited
Tyrone entered
Mason exited (ANOMALY)
Demetra entered
Latonya exited (ANOMALY)
Idella entered
Here's my code (working sample)
```
#include
#include
#include
Solution
std::unordered_map is an overkill. Since you only need to track of a presence - which is naturally boolean - std::set is enough. As a perk benefit, notice that std::set::insert indicates whether the insertion was successful or not (this is an anomaly), and std::set::erase return a number of elements erased (0 means the key was not there, another anomaly):std::set registry;
bool UserEntered(const std::string& user) {
return !registry.insert().second;
}
bool userExited(const std::string& user) {
return registry.erase(user) == 0;
}Now both methods return the anomaly:
bool anomaly;
if (action == "entered") {
anomaly = userEntered(user);
} else if(action == "exited") {
anomaly = userExited(user);
} else {
// handle error here
return;
}
LogAction(....);Code Snippets
std::set<std::string> registry;
bool UserEntered(const std::string& user) {
return !registry.insert().second;
}
bool userExited(const std::string& user) {
return registry.erase(user) == 0;
}bool anomaly;
if (action == "entered") {
anomaly = userEntered(user);
} else if(action == "exited") {
anomaly = userExited(user);
} else {
// handle error here
return;
}
LogAction(....);Context
StackExchange Code Review Q#162749, answer score: 6
Revisions (0)
No revisions yet.