简体   繁体   English

在多地图中使用timespec结构,在何处和/或如何定义运算符

[英]using timespec struct in multimap, where and/or how to define operator<

I've got some inherited code that went something like this. 我有一些继承的代码,像这样。 (Please hold the laughter... I KNOW this is ugly even after I've pseudo-codified it. That's why I'm trying to improve it and my first thing to work on is the bone headed (by my way of thinking) storing of time as a double of milliseconds since the epoch, and using that time as the key in the multimap). (请笑声。我什至知道我伪造了代码后,这还是很难看的。这就是为什么我要改进它,而我要做的第一件事就是骨头(通过我的思维方式)从纪元起将时间存储为毫秒数的两倍,并使用该时间作为多地图中的键)。 Anyway this is the "before" code, call it foo.h: 无论如何,这是“之前”代码,将其称为foo.h:

#include <vector>
#include <map>
#include <list>
#include <queue>
#include <string>
#include <time.h>
using namespace std;

#include "yasper.h"
using yasper::ptr;

class foo
{
private:
    ...
public:
    foo(...);
    virtual ~foo();
    ...
bool operator() (const ptr<foo> poFoo1, const ptr<foo> poFoo2)
    {
        ...
    }
}
...
typedef ptr<foo> fooPtr;

typedef queue<fooPtr> fooQueue;
typedef list<fooPtr> fooList;
typedef fooList *fooListPtr;

typedef multimap<double, fooPtr> fooTimeMap;

I also know that my platform doesn't provide time precision anywhere near nanoseconds but it is a little better than seconds or milliseconds. 我还知道,我的平台无法提供接近十亿分之一秒的时间精度,但比秒或毫秒要好。 Also I've tried to understand and implement the suggestions from several similar question both from google results and from this site ( C++ Using .find() with struct as key in map and using struct as key in map ). 我也试图从谷歌搜索结果和本网站( C ++使用.find()和struct作为map中的键以及使用struct作为 map中的键)来理解和实施来自几个类似问题的建议。 However those are slightly different because they are for their own new struct and I am trying to do it with an existing standard library struct. 但是,这些稍有不同,因为它们是针对自己的新结构的,我正在尝试使用现有的标准库结构来实现。

So my main goal is to change that last line to: 所以我的主要目标是将最后一行更改为:

typedef multimap<timespec, fooPtr> fooTimeMap;

When I make only that change, I get 当我仅进行更改时,我得到

In member function 'bool std::less<_Ty>::operator()(const _Ty&, const _Ty&) const [with _Ty = timespec]': /.../include/cpp/xtree:895: instantiated from 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::find(const typename _Traits::key_type&) [with _Traits = std::_Tmap_traits, std::less, std::allocator > >, true>]' /home/.../foo.cpp:109: instantiated from here /.../include/cpp/functional:136: error: no match for 'operator<' in '_Left < _Right' 在成员函数'bool std :: less <_Ty> :: operator()(const _Ty&,const _Ty&)const [with _Ty = timespec]':/.../include/cpp/xtree:895:从'std实例化:: _ Tree <_Traits> :: iterator std :: _ Tree <_Traits> :: find(const typename _Traits :: key_type&)[with _Traits = std :: _ Tmap_traits,std :: less,std :: allocator>>,true> ]'/home/.../foo.cpp:109:从此处实例化/.../include/cpp/functional:136:错误:'_Left <_Right'中的'operator <'不匹配

Based on these and other referenced posts, I've tried to define the less than operator for timespec... eg before the closing } of class foo, I put 基于这些和其他参考文章,我尝试为timespec定义小于运算符...例如,在foo类的}结束之前,

bool operator() (const timespec& lhs, const timespec& rhs)
{
    return ( lhs.tv_nsec < rhs.tv_nsec && lhs.tv_sec == rhs.tv_sec ) ||
        lhs.tv_sec < rhs.tv_sec;
}

but I guess I don't understand where is the right place to do that and why. 但是我想我不知道在哪里做这件事以及为什么。 Or even if it is not "the right place" but somewhere that is valid enough to make the multimap<timespec,...> compile (and run : - ). 或者即使它不是“正确的地方”,而是足以使multimap<timespec,...>编译(并运行multimap<timespec,...>某个地方。 Also some posts talk about defining operator<(...) and others talk about operator()(...). 也有一些文章讨论定义operator <(...),而另一些则讨论operator()(...)。

I'd rather not define a whole new wrapper class like timespec_compare_class around timespec (I've seen the syntax of multimap<timespec, fooPtr, timespec_compare_class> in other posts) so I'd rather avoid that if there is a way to do it within class foo itself, or even after the } of foo but still within foo.h. 我宁愿不围绕timespec定义一个全新的包装类,如timespec_compare_class(我在其他文章中已经看到了multimap<timespec, fooPtr, timespec_compare_class>的语法),所以我宁愿避免采用某种方法在foo类本身中,甚至在foo的}之后,但仍在foo.h中。

(note, I don't think the "bool operator() (const ptr poFoo1, const ptr poFoo2)", nor the fooQueue, fooList, nor fooListPtr are relevant to the question but I've left them in the pseudo-code just in case.) (请注意,我不认为“ bool operator()(const ptr poFoo1,const ptr poFoo2)”,也不是fooQueue,fooList或fooListPtr与问题相关,但是我将它们留在了伪代码中)如果。)

So, aside from "read a C++ primer", which I know I need to do, can anyone point me at a slightly quicker solution? 因此,除了我知道我需要做的“阅读C ++入门”之外,还有谁能为我提供一个更快的解决方案?

@thb and @MarkRansom, thanks for replying... yeah those were mentioned in other posts too though as I said slightly different cases like with their own new structs, which I tried to apply to my case. @thb和@MarkRansom,感谢您的回复……是的,虽然我说的情况与他们自己的新结构(例如,我尝试将其应用于我的结构)略有不同,但其他帖子中也提到了这些。

eg 1) When I do bool operator() (const timespec& lhs, const timespec& rhs) inside the { braces } of foo, I still get "error: no match for 'operator<' in '_Left < _Right'" 例如1)当我在foo的{大括号}中使用bool operator() (const timespec& lhs, const timespec& rhs) ,我仍然收到“错误:'_Left <_Right'中'operator <'不匹配”

eg 2) when I do that outside the braces, just before the typedef multimap, I get "bool operator()(const timespec&, const timespec&)' must be a nonstatic member function". 例如2)当我在括号之外,在typedef multimap之前,得到“ bool operator()(const timespec&,const timespec&)'必须是一个非静态成员函数”。

eg 3) When I do bool operator< (const timespec& lhs, const timespec& rhs) inside the braces, I get "'bool foo::operator<(const timespec&, const timespec&)' must take exactly one argument" And even if I changed that to one argument, I don't think that's what I want because I'm not trying to tell it how to compare a foo to a timespec. 例如3)当我在花括号内进行bool operator< (const timespec& lhs, const timespec& rhs) ,我得到“'bool foo :: operator <(const timespec&,const timespec&)'必须正好接受一个参数”即使我将其更改为一个参数,我认为这不是我想要的,因为我没有试图告诉它如何将foo与timespec进行比较。

eg 4) When I do that outside the braces, just before the typedef multimap, I get "multiple definition of `operator<(timespec const&, timespec const&)'". 例如4)当我在括号之外,在typedef multimap之前,得到“ operator <(timespec const&,timespec const&)的多重定义”。

So are one of these closer to the right track, or something completely different? 那么其中之一更接近正确轨道,还是完全不同?

Why not define the less-than operator as 为什么不将小于运算符定义为

bool operator< (const timespec& lhs, const timespec& rhs) { ... }

which differs slightly from what you wrote? 与您写的内容略有不同? The function-call operator()() can indeed be used instead of operator<(), but not quite as you did it. 实际上可以使用函数调用operator()()来代替operator <(),但不能像您所做的那样使用。 The operator<() is probably easier, anyway. 无论如何, operator <()可能更容易。

Good luck. 祝好运。

I would change it slightly more: 我会稍作更改:

struct TimeTest
{
    bool operator()(timespec const& lhs, timespec const& rhs) const
    {
         return <TEST>
    }
};
typedef multimap<timespec, fooPtr, TimeTest> fooTimeMap;

The reason that I would take the extra step is that if you define operator< for timespec is that it has a high chance for a clash with any C++ library that uses time information. 我要采取额外步骤的原因是,如果为timespec定义operator <,是因为它很可能与使用时间信息的任何C ++库发生冲突。 They may not define the same ordering and then things get complicated. 他们可能没有定义相同的顺序,然后事情变得复杂了。

By explicitly setting the comparison you guarantee there will be no clashes. 通过显式设置比较,您可以确保不会发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM