- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 QMultiMap<QDateTime, SomeOwnDataType>
我想从中检索具有特定时间戳的所有值。这就是我所做的:
QMap<QDateTime, Appointment>::iterator it = _reminders.find(now);
哪里now
值为 di 6.mrt 12:07:00 2012。这是我的循环条件:
while (it != _reminders.end() && it.key() == now) {
这是 _reminders
的状态对象:
与我的预期相反,循环被完全跳过。怎么会?
最佳答案
我认为问题在于两个时间戳不相等。如果检查 QDateTime
的 ==
运算符代码,您会发现如果时间和日期都相等,则相等。
bool QDateTime::operator==(const QDateTime &other) const
{
if (d->spec == other.d->spec && d->utcOffset == other.d->utcOffset)
return d->time == other.d->time && d->date == other.d->date;
else {
QDate date1, date2;
QTime time1, time2;
d->getUTC(date1, time1);
other.d->getUTC(date2, time2);
return time1 == time2 && date1 == date2;
}
}
但是时间相等运算符比较的是毫秒:
bool operator==(const QTime &other) const { return mds == other.mds; }
其中 mds
是以毫秒为单位的时间。在QTime
构造函数中,mds
计算如下:
mds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms;
如果你只是检查两个时间戳之间的差异是否在一个限制内,会更安全。例如:
while (it != _reminders.end() && abs(now.msecsTo(it.key())) < aLimitInMsecs) {
关于c++ - 为什么 QMultiMap 的查找操作没有像我预期的那样工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9583017/
我想使用 遍历 QMultiMap QMultiMap::const_iterator it;` 但是编译器报错 error: expected ‘;’ before ‘it’ 导致 error: ‘
我有一个 QMultiMap我想从中检索具有特定时间戳的所有值。这就是我所做的: QMap::iterator it = _reminders.find(now); 哪里now值为 di 6.mrt
我想使用QMultiMap(派生自QMap)来存储键/值对。由于我可以多次拥有 key ,因此我更喜欢使用 QMultiMap。 假设我会按给定顺序插入以下对: "C" -> 5 "A" -> 10
我应该在 QMap::insertMulti 和 QMultiMap 之间使用什么来处理: 2 -> abc 2 -> 定义 3 -> ghi 3 -> jkl 输入 2 个解决方案有什么区别? 最佳
我关注 QObject : #ifndef UETYPES #define UETYPES #include #include #include #include #include #inc
我是一名优秀的程序员,十分优秀!