处理需要在c++中一起存储的3个int类型的正确方法
Proper way to handle 3 int types needing to be stored together in c++?
所以我只是想学习一些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对象中对其进行不同排序的多个索引集合,以及根据您想要的搜索/视图类型返回不同迭代器的多个函数。
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 构造<int>具有 2 个字符串文字的向量
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 在c++中获取两个大int,并将它们存储在数组中
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- 比较两个整数在C++中与未知 int 类型的相等性
- 如何将具有两个通道的垫子转换为矢量<int><矢量>?
- 为什么 std::error_code 构造函数的第一个参数固定为 int
- 赋值和添加运算符重载(2个表和int的串联)
- 如何在一维数组中的每个元素中都有多个int值
- 命令行参数在不到 3 个 LOC 中 int?
- 打印一个带有静态 int 的函数,有一个 std::cout 和多个 std::cout 有什么区别?
- 使用 std::vector<std::future<int>> 和 std::async 启动几个线程时中止
- 如何将无符号的 int 值解释为四个字母的单词?
- 视觉C++转换和写入值为 10 的无符号 int 给出 5 个字节
- 将 int32_t 键入为 int 是个好主意吗?