c++ - C++ 中的咖啡馆订单检查器
问题描述
给定所有三个向量,编写一个函数来检查我的服务是先到先得的。所有食物都应该按照顾客要求的顺序出来。
bool isFirstComeFirstServed(const vector<int>& takeOutOrders,
const vector<int>& dineInOrders,
const vector<int>& servedOrders)
{
auto takeOutOrdersIter = takeOutOrders.cbegin();
auto dineInOrdersIter = dineInOrders.cbegin();
for (int order : servedOrders) {
// if we still have orders in takeOutOrders
// and the current order in takeOutOrders is the same
// as the current order in servedOrders
if (takeOutOrdersIter != takeOutOrders.cend() && order == *takeOutOrdersIter) {
++takeOutOrdersIter;
}
// if we still have orders in dineInOrders
// and the current order in dineInOrders is the same
// as the current order in servedOrders
else if (dineInOrdersIter != dineInOrders.cend() && order == *dineInOrdersIter) {
++dineInOrdersIter;
}
// if the current order in servedOrders doesn't match the current
// order in takeOutOrders or dineInOrders, then we're not serving first-come,
// first-served
else {
return false;
}
}
// check for any extra orders at the end of takeOutOrders or dineInOrders
if (dineInOrdersIter != dineInOrders.cend() || takeOutOrdersIter != takeOutOrders.cend()) {
return false;
}
// all orders in servedOrders have been "accounted for"
// so we're serving first-come, first-served!
return true;
}
我的疑问是我必须检查此解决方案是否适用于潜在的重复元素或订单,我检查了各种重复情况并且解决方案是否有效。你怎么看?我想百分百确定。如果那里有任何缺陷,请指导我。
解决方案
推荐阅读
- azure - Azure AD scim2 预配映射主电子邮件
- bash - Bash 循环以每秒运行 N 次命令
- reactjs - Material-UI 的登录表单模板布局混乱
- python - 基于python中的输入使用列表推导创建元组列表
- c++ - 当从 struct 寻址时,MMAP 变量的行为很奇怪
- elasticsearch - kibana watcher 中的条件警报
- python - 是否可以将图像转换为位图文本文件?
- c# - iTextSharp - 使用 C# 如何在 PDF 上放置文本
- javascript - onclick 事件侦听器无法正常工作
- python-3.x - 引用类常量 Python 3