处理需要在c++中一起存储的3个int类型的正确方法

Proper way to handle 3 int types needing to be stored together in c++?

本文关键字:int 3个 类型 方法 存储 c++ 一起 处理      更新时间:2023-10-16

所以我只是想学习一些C++,并正在创建一个实体应用程序来为一家公司预订面试。

我需要一种方法来更干净地处理事情,然后只使用5个数组。

我的数据是:

(int) Name of company member (e.g 1 John, 2 Jessica)
(int) Day of the week (e.g 1 Monday, 2 Tuesday)
(int) Timee slot for that day (e.g 1 9-10am, 2 10-11am)

现在我有这样的东西:

bool johnMondaySchedule[7] = {true, true, true, true, true, true, true};

因此,我有5个布尔数组,johnTuesday、johnThursday等。每个真值表示时隙(0=9.10am,1=10-11am)。

但我有5个客户,每个客户每周有5天的时间表,每个客户有7个时段。我觉得拥有25个布尔数组是非常低效的。

我该怎么办?我需要能够轻松地预订其中一个时隙(通过提供3个元素,对应于成员名称的int,日期和时隙。

我还需要一种简单的方法来迭代这些内容并将它们打印到屏幕上。

我研究了散列图、元组、队列,但找不到任何适合我需求的东西。

做布尔数组是唯一的方法吗?

您可能想要创建一些封装概念的类。

#include <string>
#include <set>
#include <ctime>
struct Appointment
{
  Appointment(std::string client, std::time begin, std::time duration);
  const std::string &getClient()   const { return mClient; }
  std::time          getTime()     const { return mBegin; }
  std::time          getDuration() const { return mDuration; }
  bool operator<(const Appointment &right) 
  { 
    if (mBegin != right.mBegin)
      return mBegin < right.mBegin;
    if (mClient != right.mClient)
      return mClient < right.mClient;
    return mDuration < right.mDuration;
  }
private:
  std::string mClient;
  std::time_t mBegin;
  std::time_t mDuration;
};
struct Schedule
{
  typedef coll_type::const_iterator iterator;
  bool makeAppointment(Appointment a);
  iterator begin() const { return mAppts.begin(); }
  iterator end()   const { return mAppts.end(); }
private:
  typedef std::set<Appointment> coll_type;
  coll_type mAppts;  // sorted by begin time first, client name second, duration third
};

您希望您的Schedule对象在您的约会集上强制执行不变量。像预约只能在营业时间安排,或者客户不能有两个时间重叠的预约,等等。

您可能需要多种方式为约会建立索引。因此,您可以轻松地迭代"John"(或任何其他客户)在日期X和Y之间的所有约会,或2015年3月27日(或其他任何一天)的所有约会。因此,您可能需要引用相同数据但在Schedule对象中对其进行不同排序的多个索引集合,以及根据您想要的搜索/视图类型返回不同迭代器的多个函数。

相关文章: